【问题标题】:How do I check if value already exists in the database when I insert values using C#使用 C# 插入值时如何检查数据库中是否已存在值
【发布时间】:2017-11-21 20:23:24
【问题描述】:

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("There is already Exist Rezervasyon at this Date!";

string cmd=@"SELECT COUNT(*) From RezervasyonKayıt WHERE 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");
        }

【问题讨论】:

  • 然后先做一个选择。

标签: c#


【解决方案1】:

您可以在尝试插入新行之前检查特定键/记录的存在。 例如。您可以事先执行“SELECT count(*)”类型的语句,以查看已存在具有相同值的记录。如果此计数大于零,则显示错误消息。

请记住,在高度事务性的系统中,可能会在您运行检查的那一刻和您尝试插入新记录的那一刻(几毫秒后)之间插入一条新记录。因此,您可能希望将 2 个语句包装到 1 个事务中,或者能够在代码中处理这种情况。

您还可以考虑添加一个 UNIQUE 约束以保证表中只能存在 1 条该类型的记录。 更多信息:Violation of UNIQUE KEY constraint on INSERT WHERE COUNT(*) = 0 on SQL Server 2005

【讨论】:

  • 我已经编辑了我的问题我在 int records=(int)komut.ExecuteScalar(); 上有问题System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的未处理异常
  • 这是正确的建议(将两个语句包装在一个事务中)。
  • 检查这个变量的值是什么样的:@RezTarih - 我怀疑你将它插入 SELECT 查询的方式是错误的。确保 SELECT 语句有效(尤其是 WHERE 子句)。另外为什么你需要最后两个右括号?
【解决方案2】:

您可以在插入之前添加一个选择请求,以查看您要插入的条目是否已经存在。

【讨论】:

    【解决方案3】:

    两种方法

    首先是在插入之前进行选择,例如

    SELECT COUNT(*) FROM .... WHERE .... SOMEID = SOMETHING
    

    请注意,这可能会发生竞争情况

    推荐的方法是在ID上定义一个唯一键,插入会因为重复唯一键异常而失败。捕获该异常并显示一个消息框

    【讨论】:

    • 我不推荐这种方法,因为它会导致竞争条件 - 除非您使用事务。
    • @Guilherme 感谢您的指出。编辑答案以表明唯一键是推荐的方式
    猜你喜欢
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 2011-05-16
    • 2021-10-10
    • 2020-01-21
    • 2011-12-10
    相关资源
    最近更新 更多