Cách tạo chuỗi kết nối có thểm tham khảo tượng tự như bài link
string constr = @"Server=DESKTOP-52N97T0\SQLEXPRESS;Database=todo;User Id=sa;Password=***;"; SqlConnection connection = new SqlConnection(constr); try { //Mo ket noi connection.Open(); //Chuan bi cau lenh query viet bang SQL String sqlQuery = "insert into users(email, password) values(@email, @password)"; //Tao mot Sqlcommand de thuc hien cau lenh truy van da chuan bi voi ket noi hien tai SqlCommand command = new SqlCommand(sqlQuery, connection); command.Parameters.AddWithValue("@email", emailBuilder.ToString()); command.Parameters.AddWithValue("@password", passBuilder.ToString()); //Thuc hien cau truy van va nhan ve mot doi tuong reader ho tro do du lieu int rs = command.ExecuteNonQuery(); //Su dung reader de doc tung dong du lieu //va thuc hien thao tac xu ly mong muon voi du lieu doc len if (rs != 1) { throw new Exception("Failed Query"); } } catch (Exception ex) { //xu ly khi ket noi co van de MessageBox.Show("Ket noi xay ra loi hoac doc du lieu bi loi"); } finally { //Dong ket noi sau khi thao tac ket thuc connection.Close(); }
More Stories
Game Battleship
GDI cơ bản
Cập nhật dữ liệu MS SQL từ C#