[英]How do I check if value already exists in the database when I insert values using C#
bağlanti.Open();
SqlCommand komut = new SqlCommand("insert into RezervasyonKayıt (Rezervasyon_İçerik,Yemek_Tercihi,Kişi_Sayısı,İçecek_Tercihi,Giris_Ekstraları,Yetkili_Kisi,Rezervasyon_Tarihi)values(@İçerik,@Yemek,@Kisi,@İcecek,@Giris,@Yetkili,@RezTarih)", bağlanti);
komut.Parameters.AddWithValue("@İçerik", comboBox1.SelectedItem);
komut.Parameters.AddWithValue("@Yemek", comboBox2.SelectedItem);
komut.Parameters.AddWithValue("@Kisi", comboBox3.SelectedItem);
komut.Parameters.AddWithValue("@İcecek", comboBox4.SelectedItem);
komut.Parameters.AddWithValue("@Giris", comboBox5.SelectedItem);
komut.Parameters.AddWithValue("@Yetkili", comboBox6.SelectedItem);
komut.Parameters.AddWithValue("@RezTarih", dateTimePicker1.Value);
komut.ExecuteNonQuery();
comboBox1.Text = "";
comboBox2.Text = "";
comboBox3.Text = "";
comboBox4.Text = "";
comboBox5.Text = "";
comboBox6.Text = "";
bağlanti.Close();
XtraMessageBox.Show("Randevu Başarıyla Kayıt Edilmiştir", "Bilgi Mesajı", MessageBoxButtons.OK, MessageBoxIcon.Information);
我試圖只檢查Rezervasyon_Tarihi是否存在於數據庫中,我將顯示給MessageBox(“在此日期已經存在Rezervasyon_”!
字符串cmd = @“ SELECT COUNT(*)來自RezervasyonKayıt,其中Rezervasyon_Tarihi = @ RezTarih))”;
komut=new SqlCommand(cmd,bağlanti);
komut.Parameters.AddWithValue("@RezTarih",dateTimePicker1.Value);
bağlanti.Open();
int records=(int)komut.ExecuteScalar();
if (records==0)
{
komut.Parameters.Clear();
cmd=@"insert into RezervasyonKayıt (Rezervasyon_İçerik,Yemek_Tercihi,Kişi_Sayısı,İçecek_Tercihi,Giris_Ekstraları,Yetkili_Kisi,Rezervasyon_Tarihi)values(@İçerik,@Yemek,@Kisi,@İcecek,@Giris,@Yetkili,@RezTarih)";
komut=new SqlCommand(cmd,bağlanti);
komut.Parameters.AddWithValue("@İçerik", comboBox1.SelectedItem);
komut.Parameters.AddWithValue("@Yemek", comboBox2.SelectedItem);
komut.Parameters.AddWithValue("@Kisi", comboBox3.SelectedItem);
komut.Parameters.AddWithValue("@İcecek", comboBox4.SelectedItem);
komut.Parameters.AddWithValue("@Giris", comboBox5.SelectedItem);
komut.Parameters.AddWithValue("@Yetkili", comboBox6.SelectedItem);
komut.Parameters.AddWithValue("@RezTarih", dateTimePicker1.Value);
komut.ExecuteNonQuery();
comboBox1.Text = "";
comboBox2.Text = "";
comboBox3.Text = "";
comboBox4.Text = "";
comboBox5.Text = "";
comboBox6.Text = "";
XtraMessageBox.Show("Randevu Başarıyla Kayıt Edilmiştir", "Bilgi Mesajı", MessageBoxButtons.OK, MessageBoxIcon.Information);
}else
{
Response.Write("Records Exists");
}
您可以在插入之前添加選擇請求,以查看要插入的條目是否已經存在。
兩種方法可以做到
首先是在插入之前進行選擇,例如
SELECT COUNT(*) FROM .... WHERE .... SOMEID = SOMETHING
請注意,這種情況可能會發生競爭
推薦的方法是在ID上定義一個唯一鍵,由於重復的唯一鍵異常,插入將失敗。 捕獲該異常並顯示msgbox
您可以在嘗試插入新行之前檢查特定鍵/記錄的存在。 例如,您可以預先執行“ SELECT count(*)”類型的語句,以查看具有相同值的記錄已經存在。 如果此計數大於零,則顯示錯誤消息。
請記住,在高度事務處理的系統中,在您運行支票的那一刻與嘗試插入新記錄的那一刻(幾毫秒后)之間可能會插入一條新記錄。 因此,您可能希望將2條語句包裝到1個事務中,或者能夠在代碼中處理這種情況。
您也可以考慮添加UNIQUE約束,以確保表中只能存在1條此類記錄。 詳細信息: 在SQL Server 2005的INSERT WHERE COUNT(*)= 0上違反UNIQUE KEY約束
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.