C,
pasted
on Jun 15:
|
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();
}
|
Output:
|
Line 1: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'SqlConnection'
Line 2: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'DataTable'
Line 3: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'SqlDataAdapter'
Line 5: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'void'
Line 8: error: stray '@' in program
Line 20: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'void'
Line 27: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'void'
Line 41: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'void'
Line 53: error: stray '@' in program
Line 53: error: missing terminating " character
Line 54: error: stray '@' in program
Line 54: error: stray '\341' in program
Line 54: error: stray '\273' in program
Line 54: error: stray '\215' in program
Line 54: error: stray '\303' in program
Line 54: error: stray '\252' in program
Line 54: error: stray '@' in program
Line 54: error: stray '\341' in program
Line 54: error: stray '\273' in program
Line 54: error: stray '\233' in program
Line 54: error: stray '\303' in program
Line 54: error: stray '\255' in program
Line 54: error: missing terminating " character
|
|