简体   繁体   English

使用C#插入值时,如何检查数据库中是否已存在值

[英]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.

相关问题 如何检查我尝试插入数据库中的值已存在 - How to check value that I am trying to insert in database already exists 如何在SQLite中检查数据库是否存在C# - How do I check in SQLite whether a database exists C# 如何使用C#检查列表中是否存在值 - How can I check if a value exists in a list using C# 如何检查数据库中是否已经存在用户名 - How do I check if a user name already exists in Database 如何检查记录是否已存在于SQL数据库中? - How do I check if the record already exists in SQL database? 在使用IDataErrorInfo在MVVM中执行验证时,我应该在哪里执行检查以查看数据库中是否已存在值? - Where should I perform a check to see if a value already exists in the database when performing validation in MVVM using IDataErrorInfo? 如何使用 C# 检查对象(我必须插入到列表中)是否已经在列表中? - How can I check if an object (that I have to insert in a list) is already in the list using C#? 如何使用 c# 检查值是否已存在于数据库中以实现统一(SQL 语法错误) - How to check if value already exists in database with c# for unity (error in SQL syntax) 如何在C#中检查数据库中是否已经存在数据 - How to check if data already exists in database in c# 如何检查C#中是否存在列表索引? - How do I check to see if a list index exists in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM