当我使用C#插入值时,如何检查数据库中是否已存在值 [英] How I check if value is already exist in database when I insert values using C#

查看:103
本文介绍了当我使用C#插入值时,如何检查数据库中是否已存在值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bağlanti.Open();

SqlCommand komut = new SqlCommand(插入RezervasyonKayıt(Rezervasyon_İçerik,Yemek_Tercihi,Kişi_Sayısı,İçecek_Tercihi,Giris_Ekstraları,Yetkili_Kisi,Rezervasyon_Tarihi)值(@İç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 =;
$ b $bbağlanti.Close();

XtraMessageBox.Show(RandevuBaşarıylaKayıtEdilmiştir,BilgiMesajı,MessageBoxButtons.OK,MessageBoxIcon.Information);



我是什么尝试过:



保存在数据库中的值检查数据库中是否存在Rezervasyon_Tarihi





 string cmd = @SELECT COUNT(*)来自RezervasyonKayıtWHERERezervasyon_Tarihi = @ RezTarih)); 
SqlCommand komut = new SqlCommand(cmd,bağlanti);
komut.Parameters.AddWithValue(@ RezTarih,dateTimePicker1.Value); $ b $bbağlanti.Open();
int records =(int)komut.ExecuteScalar();

if(records == 0)
{
komut.Parameters.Clear();
cmd = @插入RezervasyonKayıt(Rezervasyon_İçerik,Yemek_Tercihi,Kişi_Sayısı,İçecek_Tercihi,Giris_Ekstraları,Yetkili_Kisi,Rezervasyon_Tarihi)值(@İç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(RandevuBaşarıylaKayıtEdilmiştir,BilgiMesajı,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
MessageBox.Show(已经存在!);
}

解决方案

我建​​议你改变你的sql并使其成为存储过程。用这种方式写起来会容易得多。在存储过程中,您可以使用

  IF   EXISTS  SELECT   1   FROM  < span class =code-keyword> table   WHERE  field =  @ value 
BEGIN
- 执行更新
END
ELSE
BEGIN
- 执行插入
END





或者您可以使用MERGE语句。 MERGE(Transact-SQL)| Microsoft Docs [ ^ ]


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);

What I have tried:

before value saved in database check if Rezervasyon_Tarihi is exist in database


string cmd = @"SELECT COUNT(*) From RezervasyonKayıt WHERE Rezervasyon_Tarihi=@RezTarih))";
         SqlCommand 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
          {
              MessageBox.Show("Already Exist !");
          }

解决方案

I suggest you change your sql and make it a stored procedure. It will be much easier to write that way. In the stored procedure you can use

IF EXISTS (SELECT 1 FROM table WHERE field = @value)
  BEGIN
    -- do the update
  END
ELSE
  BEGIN
   -- do the insert
  END 



Or you can use the MERGE statement. MERGE (Transact-SQL) | Microsoft Docs[^]


这篇关于当我使用C#插入值时,如何检查数据库中是否已存在值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆