part 6 kết nối với cơ sở dữ liệu database c# SqlDataReader và NextResult (đọc nhiều bản ))

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace ExecuteXmlReader_chạy_1_lúc_nhiều_bản { class Program { static void Main(string[] args) { //Quá trình khởi tạo sự kết nối vbới co sở dử liệu database // câu lệnh nằm trong SqlConnection () có trong hệ thống rùi nha SqlConnection connet = new SqlConnection(@"Data Source=QGPDGJK8S6DWF6S;Initial Catalog=Qlhocsinh;Integrated Security=True"); Console.Write("\nQua trinh dang dk ket noi !"); try { connet.Open(); /// mở kết nối Console.Write("\nKet noi thanh cong!"); /// ta tạo 3 chuổi đọc nah string docbanlophoc = @"select * from HocSinh ";// đọc bản học sinh string docbanmonhoc = @"select * from MonHocc ";// đọc bản monhoc string docbanhocsinh = @"select * from HocSinh ";// đọc bản hocsinh string tongbaban = docbanlophoc + docbanmonhoc + docbanhocsinh;// kết ;nối 3 bản lại SqlCommand ketnoi = new SqlCommand(tongbaban, connet);/// thiết lập mối kết nối giữa chuổi với database Console.Write("\nLenh thuc thi dang dk khoi tao!"); SqlDataReader doc = ketnoi.ExecuteReader();/// đã kết nối câu lệnh thực lthi thành công với database thì tạo 1 biến thcự hiện việc đọc int diem = 1; do { Console.Write("\nThong tin ban thu: " + diem++); while (doc.Read())// đọc hết bản thứ nhất qua bản thứ 2 { Console.Write("\n{0}--------{1}\n", doc[0], doc[1]); } Console.Write("\n----------------------------------------------------\n"); } while (doc.NextResult());/// lệnh qua bản thứ 2 } catch(SqlException e) { Console.WriteLine(e.ToString()); } finally { Console.Write("\nNgat ket noi!"); connet.Close(); } Console.ReadKey(); } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.