[英]How do I check if value already exists in the database when I insert values using C#
bağlanti.Open(); 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);
Im trying to check only Rezervasyon_Tarihi if is it exist in database I will display to MessageBox("There is already Exist Rezervasyon at this Date! "; 我试图只检查Rezervasyon_Tarihi是否存在于数据库中,我将显示给MessageBox(“在此日期已经存在Rezervasyon_”!
string cmd=@"SELECT COUNT(*) From RezervasyonKayıt WHERE Rezervasyon_Tarihi=@RezTarih))"; 字符串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");
}
您可以在插入之前添加选择请求,以查看要插入的条目是否已经存在。
Two ways you can do it 两种方法可以做到
First is to do a select before you insert, something like 首先是在插入之前进行选择,例如
SELECT COUNT(*) FROM .... WHERE .... SOMEID = SOMETHING
Do note that race condition could happen for this 请注意,这种情况可能会发生竞争
The recommended way is to define a unique key on the ID and the insert will fail due to duplicated unique key exception. 推荐的方法是在ID上定义一个唯一键,由于重复的唯一键异常,插入将失败。 Catch that exception and display a msgbox
捕获该异常并显示msgbox
You can check the presence of a particular key/record before trying to INSERT a new row. 您可以在尝试插入新行之前检查特定键/记录的存在。 Eg you can execute a "SELECT count(*)" type of statement beforehand to see records with the same value already exist.
例如,您可以预先执行“ SELECT count(*)”类型的语句,以查看具有相同值的记录已经存在。 If this count is greater than zero then you display your error message.
如果此计数大于零,则显示错误消息。
Keep in mind that in highly transactional systems there might be a new record inserted between the moment you run the check and the moment (a few milliseconds later) when you are trying to insert a new record. 请记住,在高度事务处理的系统中,在您运行支票的那一刻与尝试插入新记录的那一刻(几毫秒后)之间可能会插入一条新记录。 So you might want to either wrap 2 statements into 1 transaction or be able to handle this scenario in code.
因此,您可能希望将2条语句包装到1个事务中,或者能够在代码中处理这种情况。
You may also consider adding a UNIQUE constraint to guarantee that only 1 record of that kind can exist in the table. 您也可以考虑添加UNIQUE约束,以确保表中只能存在1条此类记录。 More info: Violation of UNIQUE KEY constraint on INSERT WHERE COUNT(*) = 0 on SQL Server 2005
详细信息: 在SQL Server 2005的INSERT WHERE COUNT(*)= 0上违反UNIQUE KEY约束
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.