codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
private SqlConnection con; private DataTable dt = new DataTable("Luu"); private SqlDataAdapter da = new SqlDataAdapter(); private void connect() { //(local):tên sever QuanLy: tên DB Luu: tên table String cn = @"Data Source=(local);Initial Catalog=QuanLy;Integrated Security=True"; try { con = new SqlConnection(cn); con.Open(); //Mở kết nối } catch (Exception ex) { MessageBox.Show("Không thể kết nối tới dữ liệu!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void disconnect() { con.Close(); //Đóng kết nối con.Dispose(); //Giải phóng tài nguyên con = null; //Hủy đối tượng } private void getdata() { SqlCommand command = new SqlCommand(); //Khai báo 1 command command.Connection = con; //Kết nối command.CommandType = CommandType.Text; //Khai báo kiểu command command.CommandText = "Select * From Luu"; da.SelectCommand = command; da.Fill(dt); dataGridViewX1.DataSource = dt; } //lưu dữ liệu đã nhập từ các textBox vào DB private void btnLuu_Click(object sender, EventArgs e) { DataRow row = dt.NewRow(); row["HọTên"] = txtHoten.Text; row["GiớiTính"] = txtGioi.Text; dt.Rows.Add(row); SqlCommand cmdInsert = new SqlCommand(); cmdInsert.Connection = con; cmdInsert.CommandType = CommandType.Text; cmdInsert.CommandText = @"INSERT INTO DanhSach2 ( HọTên, GiớiTính) VALUES ( @HọTên, @GiớiTính) "; cmdInsert.Parameters.Add("@HọTên", SqlDbType.NVarChar, 50, "HọTên"); cmdInsert.Parameters.Add("@GiớiTính", SqlDbType.NVarChar, 50, "GiớiTính"); da.InsertCommand = cmdInsert; da.Update(dt); MessageBox.Show("Bạn đã thêm thành công!", "THÔNG BÁO", MessageBoxButtons.OK); Close(); disconnect(); Dispose(); }
Private
[
?
]
Run code
Submit