Admin 838 Đã viết 4/01/2019 Chào các bạn, hôm nay mình tiếp tục serial về kết nối cơ sở dữ liệu SQL SERVER 2017 :) . Nếu các bạn đọc bài này thì chắc chắn là người mới sử dụng rồi, vậy nên các bạn chưa đọc cách kết nối SQL SERVER trên CSharp thì hãy đọc bài đó trước nhé. Oke, sau khi các bạn kết nối, chúng ta sẽ đi vào phần đọc các bản ghi đã có trong database của chúng ta. Giả sử chúng ta đã có 1 database như sau :Tên database : TeamCodeDaoBảng UserTCD : bao gồm cột STT (int), cột Tên (nvachar(250)), cột Nhóm (nvchar(250)), cột SĐT(nvchar(50)) Các dữ liệu sẽ có như sau : Rồi, giờ chúng ta bắt đầu vào việc xử lý chính, các bạn không quên sử dụng thư viện có sẵn của Microsoft nhé : using System.Data.SqlClient. Trước hết ta tạo kết nối như bài hướng dẫn kết nối SQL SERVER : try { string ConnectionString = @"Data Source=TeamCodeDao;Database=databaseTCD; User ID=sa;Password=123456"; SqlConnection KetNoi = new SqlConnection(ConnectionString) MessageBox.Show("Kết nối thành công"); KetNoi.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Kết nối không thành công"); } Trích dẫn Chia sẻ bài đăng này Link tới bài viết Share on other sites
Admin 838 Đã viết 5/01/2019 Phần 1 : Đọc dữ liệu của bảng ghi (Tớ có 3-4 mẫu, các bạn có thể chọn 1 trong các mẫu đó nhé.) Cách 1 : Đọc 1 giá trị trong bảng ghi khi nhập kiểu số SqlCommand Lenh = new SqlCommand(); // Khởi tạo lệnh Lenh.CommandType = CommandType.Text; // Lệnh dạng text Lenh.CommandText = "Select Count(*) From UserTCD"; // nhập câu lệnh của SQL vào (cái này các bạn phải học thêm 1 khoá SQL SERVER nữa) : lệnh này sẽ lấy tổng các bảng ghi có trong data UserTCD. Lenh.Connection = KetNoi; object Data = Lenh.ExecuteScalar(); // execute lệnh nhé int N = (int)Data; MessageBox.Show("Có " + N.ToString() + " liên hệ"); // như dữ liệu trên thì nó sẽ hiển thị có 6 liên hệ Lenh.CommandText = "Select * From UserTCD where STT = 3"; // nhập câu lệnh SQL lấy giá trị của hàng có STT là 3 ở bảng ghi UserTCD Lenh.Connection = KetNoi; SqlDataReader SQL_Reader = Lenh.ExecuteReader(); // execute nó ra if (SQL_Reader.Read()) { MessageBox.Show("Liên hệ : " + SQL_Reader.GetString(1)); // như trong bảng thì hàng có stt là 3 và cột thứ 1 thì là Mod nhé :) } SQL_Reader.Close(); // sau khi chạy xong nhớ đóng lại KetNoi.Close(); // sau khi chạy xong lệnh nhớ đóng lại Cách 2 : Đọc 1 giá trị trong bảng ghi khi nhập kiểu chuỗi SqlCommand Lenh = new SqlCommand(); Lenh.CommandType = CommandType.Text; Lenh.CommandText = "Select Count(*) From UserTCD"; Lenh.Connection = KetNoi; object Data = Lenh.ExecuteScalar(); int N = (int)Data; MessageBox.Show("Có " + N.ToString() + " liên hệ"); // tương tự như trên, nhưng đoạn sau sẽ khác khi mà đọc kiểu chuỗi Lenh.CommandText = "Select * From UserTCD where Tên = 'Mod'"; //Phần nhập chuỗi phải đặt trong dấu nháy đơn 'chuỗi' //Cái này ở trong SQL SERVER mới dạy, ở bài này tớ chỉ hướng dẫn sử dụng //SQLS SERVER với C# thôi nhé. Lenh.Connection = KetNoi; SqlDataReader SQL_Reader = Lenh.ExecuteReader(); if (SQL_Reader.Read()) { MessageBox.Show("Lien he : " + SQL_Reader.GetString(2)); // ở đây tớ đổi thành getstring(2) để lấy giá trị thứ 3 trong cột Mod của bảng ghi nhé -> Moderator } SQL_Reader.Close(); KetNoi.Close(); // xong xuôi vẫn nhớ phải đóng lại, khi cần thì phải mở lại nhé. Cách 3 : Đọc lần lượt các giá trị trong bảng ghi theo cột SqlCommand Lenh = new SqlCommand(); Lenh.CommandType = CommandType.Text; Lenh.CommandText = "Select Count(*) From UserTCD"; Lenh.Connection = KetNoi; object Data = Lenh.ExecuteScalar(); int N = (int)Data; MessageBox.Show("Có " + N.ToString() + " liên hệ"); Lenh.CommandText = "Select * From UserTCD"; Lenh.Connection = KetNoi; SqlDataReader SQL_Reader = Lenh.ExecuteReader(); while (SQL_Reader.Read()) { MessageBox.Show("Lien he : " + SQL_Reader.GetString(1)); // Ở vòng lặp này sẽ đọc lần lượt các giá trị ở cột thứ 2 trong bảng ghi UserTCD } SQL_Reader.Close(); KetNoi.Close(); Cách 4 : Đọc lần lượt các giá trị trong bảng ghi theo cột có điều kiện : Mẫu 1 : if (KetNoi.State == ConnectionState.Closed) { KetNoi.Open(); //ket noi dong -> mo ket noi } SqlCommand Lenh = new SqlCommand(); Lenh.CommandType = CommandType.Text; Lenh.CommandText = "Select * From UserTCD Where Nhóm='Member'; // Đây là phần thêm điều kiện là chuỗi thì sẽ có dấu ' ' nhé. Lenh.Connection = KetNoi; SqlDataReader SQL_Reader = Lenh.ExecuteReader(); while (SQL_Reader.Read()) { MessageBox.Show("Lien he : " + SQL_Reader.GetString(1)); // vẫn là đọc cột thứ 2 trong bảng ghi. } SQL_Reader.Close(); KetNoi.Close(); Mẫu 2 : if (KetNoi.State == ConnectionState.Closed) { KetNoi.Open(); //ket noi dong -> mo ket noi } SqlCommand Lenh = new SqlCommand(); Lenh.CommandType = CommandType.Text; Lenh.CommandText = "Select * From Contact Where Nhom=@MaNhom"; Lenh.Connection = KetNoi; SqlParameter ParamSql = new SqlParameter("@MaNhom", SqlDbType.NVarChar); ParamSql.Value = "Member"; Lenh.Parameters.Add(ParamSql); SqlDataReader SQL_Reader = Lenh.ExecuteReader(); while (SQL_Reader.Read()) { MessageBox.Show("Lien he : " + SQL_Reader.GetString(1)); } SQL_Reader.Close(); KetNoi.Close(); Trích dẫn Chia sẻ bài đăng này Link tới bài viết Share on other sites
Admin 838 Đã viết 19/01/2019 Phần 2 : Thêm dữ liệu Trước hết các bạn cần biết câu lệnh để thêm dữ liệu trong SQL SERVER : Insert into Contact(STT,Ten,Email,Phone,Nhom) values("10,N'Tên Tiếng Việt','teamcodedao.com@gmail.com',84904942991,N'Tên nhóm tiếng việt'") * Chú ý là câu lệnh SQL muốn viết tiếng việt thì phải thêm 'N' vào đằng trước nhé. Cách 1 : SqlCommand Lenh = new SqlCommand(); Lenh.CommandType = CommandType.Text; Lenh.CommandText = "Insert into Contact(STT,Ten,Email,Phone,Nhom) values("10,N'Tên Tiếng Việt','teamcodedao.com@gmail.com',84904942991,N'Tên nhóm tiếng việt')"; Lenh.Connection = KetNoi; int Ret = Lenh.ExecuteNonQuery(); if (Ret > 0) { MessageBox.Show("Okay"); } else { MessageBox.Show("Failed"); } KetNoi.Close(); Cách 2 : SqlCommand Lenh = new SqlCommand(); Lenh.CommandType = CommandType.Text; string SqlLenh = "Insert into Contact(STT,Ten,Email,Phone,Nhom) values(@STT,@Ten,@Email,@Phone,@Nhom)"; Lenh.CommandText = SqlLenh; Lenh.Connection = KetNoi; Lenh.Parameters.Add("@STT", SqlDbType.Int).Value = 10; Lenh.Parameters.Add("@Ten", SqlDbType.NVarChar).Value = "Tên Tiếng Việt"; Lenh.Parameters.Add("@Email", SqlDbType.NVarChar).Value = "banquantri@teamcodedao.com"; Lenh.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = "84904942991"; Lenh.Parameters.Add("@Nhom", SqlDbType.NVarChar).Value = "Tên nhóm tiếng việt"; int Ret = Lenh.ExecuteNonQuery(); if (Ret > 0) { MessageBox.Show("Okay"); } else { MessageBox.Show("Failed"); } KetNoi.Close(); Trích dẫn Chia sẻ bài đăng này Link tới bài viết Share on other sites
Admin 838 Đã viết 19/01/2019 Phần 3 : Sửa dữ liệu bảng ghi Tương tự, đây là mẫu truy vấn sửa dữ liệu bảng ghi trong SQL SERVER : UPDATE Contact SET Ten=N'Tên cần đổi',Phone='Số điện thoại đổi' Where STT=1 Cách 1 : SqlCommand Lenh = new SqlCommand(); Lenh.CommandType = CommandType.Text; string SqlLenh = "Update Contact set Ten=N'Tên cần đổi',Phone='Số điện thoại đổi' Where STT =1"; Lenh.CommandText = SqlLenh; Lenh.Connection = KetNoi; int Ret = Lenh.ExecuteNonQuery(); if (Ret > 0) { MessageBox.Show("Okay"); } else { MessageBox.Show("Failed"); } KetNoi.Close(); Cách 2 : SqlCommand Lenh = new SqlCommand(); Lenh.CommandType = CommandType.Text; string SqlLenh = "Update Contact set Ten=@Ten,Phone=@Phone,Email=@Email Where STT =@STT"; Lenh.CommandText = SqlLenh; Lenh.Connection = KetNoi; Lenh.Parameters.Add("@STT", SqlDbType.Int).Value = 1; Lenh.Parameters.Add("@Ten", SqlDbType.NVarChar).Value = "Tên cẩn đổi"; Lenh.Parameters.Add("@Email", SqlDbType.NVarChar).Value = "email cần đổi"; Lenh.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = "Số phone cần đổi"; //Lenh.Parameters.Add("@Nhom", SqlDbType.NVarChar).Value = "Tên nhóm cần đổi"; int Ret = Lenh.ExecuteNonQuery(); if (Ret > 0) { MessageBox.Show("Okay"); } else { MessageBox.Show("Failed"); } KetNoi.Close(); Trích dẫn Chia sẻ bài đăng này Link tới bài viết Share on other sites
Admin 838 Đã viết 19/01/2019 Phần 4 : Xóa dữ liệu của bản ghi Mẫu xóa dữ liệu của bản ghi SQL SERVER : Delete from Contact Where STT=2 Mọe, làm thì khó chứ, đập thì nhanh gọn lẹ vãi Tất nhiên khi đã thì phải bao gồm theo điều kiện xóa, ở đây là tớ xóa theo cột STT có số thứ tự là 2 nhé :). Cách 1 : SqlCommand Lenh = new SqlCommand(); Lenh.CommandType = CommandType.Text; string SqlLenh = "Delete from Contact Where STT=2"; Lenh.CommandText = SqlLenh; Lenh.Connection = KetNoi; int Ret = Lenh.ExecuteNonQuery(); if (Ret > 0) { MessageBox.Show("Okay"); } else { MessageBox.Show("Failed"); } KetNoi.Close(); Cách 2 : SqlCommand Lenh = new SqlCommand(); Lenh.CommandType = CommandType.Text; string SqlLenh = "Delete from Contact Where STT=@STT"; Lenh.CommandText = SqlLenh; Lenh.Connection = KetNoi; Lenh.Parameters.Add("@STT", SqlDbType.Int).Value = 2; //Lenh.Parameters.Add("@Ten", SqlDbType.NVarChar).Value = "Tên"; //Lenh.Parameters.Add("@Email", SqlDbType.NVarChar).Value = "Email"; //Lenh.Parameters.Add("@Phone", SqlDbType.NVarChar).Value = "Số Phone"; //Lenh.Parameters.Add("@Nhom", SqlDbType.NVarChar).Value = "Nhóm"; int Ret = Lenh.ExecuteNonQuery(); if (Ret > 0) { MessageBox.Show("Okay"); } else { MessageBox.Show("Failed"); } KetNoi.Close(); Kết thúc sơ bộ cơ bản các lệnh và các thao tác cơ bản nhất trong SQL SERVER sử dụng CSharp. Các bạn có bổ sung gì thêm vui lòng reply ngay bên dưới nhé. Cám ơn các bạn đã theo dõi. Trích dẫn Chia sẻ bài đăng này Link tới bài viết Share on other sites