如何将行插入Mysql数据库而不重复?

时间:2015-06-28 22:17:16

标签: c# mysql duplicates

当我想在我的MySql数据库中插入一行时,将插入4行而不是1.你能帮帮我吗?

    MySqlConnection con = new MySqlConnection(cs);
                    string Query = "Insert Into csokolade (márka,ár,darab) values('" + this.Marka_Textbox.Text + "','" + this.Ar_Textbox.Text + "','" + this.Darab_Textbox.Text + "')";
                    MySqlCommand com = new MySqlCommand(Query, con);
                    com.ExecuteNonQuery();

编辑:以下是必须插入行的代码部分:

    try
            {
        MySqlConnection con = new MySqlConnection(cs);
                con.Open();
                int a = 0;
                string q = "select count(*) from csokolade where márka='" + this.Marka_Textbox.Text + "';";
                MySqlCommand com = new MySqlCommand(q, con);

                try
                {
                    a = Convert.ToInt32(com.ExecuteScalar());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                if (a == 0)
                {
                    string Query = "Insert Into csokolade (márka,ár,darab) values('" + this.Marka_Textbox.Text + "','" + this.Ar_Textbox.Text + "','" + this.Darab_Textbox.Text + "')";
                    com = new MySqlCommand(Query, con);
                    com.ExecuteNonQuery();
                }
                else
                {
                    MessageBox.Show("Ez a rekord már létezik!");
                    return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

0 个答案:

没有答案