//CHECK IF COMPLETE DUPLICATE
query = "SELECT * FROM TBL_FLAVORS WHERE flavor_name = @flavor_name AND flavor_supplierid = @supplier_id";
using (SqlConnection con = new SqlConnection(connstring))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(query, con))
{
SqlDataReader read;
cmd.Parameters.AddWithValue("@flavor_name", txtFlavorName.Text.ToString());
cmd.Parameters.AddWithValue("@supplier_id", supplierid);
read = cmd.ExecuteReader();
if (read.Read())
{
MessageBox.Show("This flavor for this supplier already exists.");
return;
}
read.Close();
}
}
//CHECK IF ALMOST SIMILAR
//IF RECORD FOUND, RETURN
//OTHERWISE, PROCEED TO UPDATE/INSERT
query = "SELECT * FROM TBL_FLAVORS WHERE (flavor_name LIKE @flavor_name OR flavor_name LIKE @flavor_name2 OR FLAVOR_NAME LIKE @flavor_name3) AND flavor_supplierid = @supplier_id";
using (SqlConnection con = new SqlConnection(connstring))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(query, con))
{
string flavorname = "%" + txtFlavorName.Text.ToString()+ "%";
string flavorname2 = "" + txtFlavorName.Text.ToString() + "%";
string flavorname3 = "%" + txtFlavorName.Text.ToString() + "";
SqlDataReader read;
cmd.Parameters.AddWithValue("@flavor_name", flavorname);
cmd.Parameters.AddWithValue("@flavor_name2", flavorname2);
cmd.Parameters.AddWithValue("@flavor_name3", flavorname3);
cmd.Parameters.AddWithValue("@supplier_id", supplierid);
read = cmd.ExecuteReader();
if (read.Read())
{
DialogResult dialog = MessageBox.Show("It is possible that this flavor for this supplier already exists. Do you want to continue?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialog == DialogResult.No)
{
return;
}
}
read.Close();
}
}
小組成員詢問如果已經有一個「英寸」並且用戶輸入了「innch」。它應該被認爲是重複的。或者說數據庫中有一個'巧克力',如果用戶輸入'巧克力',它應該要求確認。如何防止SQL數據庫中的類似(重複)條目?
哦,對,正確,謝謝指出(冗餘)! :)我會檢查出鏈接:) – herondale