01/10/2018, 12:28
Tại sao đã kiểm tra phần tử trùng mà vẫn thêm phần tử đó vào
Em có CSDL là tên là chunha với các trường thuộc tính chunha,cmt,tuoi,nghenghiep,gioitinh,sdt và sonha. Em làm code kiểm tra trùng là Kiemtr() và một code thêm dữ liệu như vậy mà sao nó vẫn thêm mặc dù đã trùng cái sn
private Boolean Kiemtr()
{
Con.Open();
bool kt = true;
string sqldoc = "select * from chunha";
SqlCommand cmd = new SqlCommand(sqldoc, Con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (sonha.Text == dr[6].ToString())
{
kt = false;
break;
}
}
Con.Close();
return kt;
}``
``
if (Kiemtr()==false)
{
MessageBox.Show("Nhà đã có chủ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
chunha.Focus();
}
else
{
Con.Open();
String sqlthem = "Insert into chunha values('" + chunha.Text + "','" + cmt.Text + "','" + tuoi.Text + "','" + gioitinh.Text + "','" + nghenghiep.Text + "','" + sdt.Text + "','" + sonha.Text + "')";
SqlCommand cmd = new SqlCommand(sqlthem, Con);
cmd.ExecuteNonQuery();
MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
cn.Focus();
Con.Close();
hienthi();
}
Bài liên quan
Đọc đây thử xem
c-sharpcorner.com
Difference Between Equality Operator ( ==) and Equals() Method in C#
Both the == Operator and the Equals() method are used to compare two value type data items or reference type data items. This article explains the basic difference between these two.