检查数据表中的现有条目

时间:2014-03-15 18:21:26

标签: c# mysql database datatable

private void txtBarcode_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                string connString = "Server=192.168.1.100;Database=product;Uid=newuser;Pwd=password";
                MySqlConnection conn = new MySqlConnection(connString);
                    //cmd = conn.CreateCommand();
                    //cmd.CommandText = "Select * From tblindividualproduct";
                    if (e.KeyCode == Keys.Enter)
                    {
                        if (txtBarcode.Text == "")
                        {
                            MessageBox.Show("Please Fill the correct ProductID");
                        }
                        else
                        {
                            if (HasRecord("tblindividualproduct.ProductID", connString) == false)
                            {
                                string sql = "Select Iproduct.ProductId, prodInfo.Name,Iproduct.UpdatedPrice From product.tblproductinformation AS prodInfo INNER JOIN product.tblindividualproduct AS Iproduct ON prodInfo.Code = Iproduct.Code where Iproduct.ProductID = @idText";
                                using (var adapt = new MySqlDataAdapter(sql, conn))
                                using (var cmd = new MySqlCommandBuilder(adapt)) //Not sure what you need this for unless you are going to update the database later.
                                {
                                    adapt.SelectCommand.Parameters.AddWithValue("@idText", txtBarcode.Text);
                                    adapt.Fill(dt);
                                    dgItems.ReadOnly = true;
                                    dgItems.DataSource = dt;
                                }
                            }
                        }
                    }
               }
            catch (MySqlException ex)
            {
                // output the error to see what's going on
                MessageBox.Show(ex.Message);
            }
        }

        static public bool HasRecord(string ProductID, string connString)
        {
            //add try catches where required
            bool foundRecord = true;
            Int32 numRecords = 0;
            string sql =
                "Select count(Iproduct.ProductId, prodInfo.Name,Iproduct.UpdatedPrice) From product.tblproductinformation AS prodInfo INNER JOIN product.tblindividualproduct AS Iproduct ON prodInfo.Code = Iproduct.Code where Iproduct.ProductID = @idText";
            using (MySqlConnection conn = new MySqlConnection(connString))
            {
                MySqlCommand cmd = new MySqlCommand(sql, conn);
                cmd.Parameters.Add("@idText", SqlDbType.VarChar);
                cmd.Parameters["@idText"].Value = ProductID;
                try
                {
                    conn.Open();
                    numRecords = (Int32)cmd.ExecuteScalar();
                    if (numRecords == 0) foundRecord = false;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
            return foundRecord;
        }

我使用此代码在我的数据集中添加项目,你能帮助我检查输入,如果数据集上有重复的条目,你会不会接受它吗?

编辑:我已更新我的代码,以向您展示我目前的工作。

1 个答案:

答案 0 :(得分:0)

实际的方法是在数据库表ProductID中将PrimaryKey设为tblindividualproduct,然后您就可以在代码中处理异常。

但是,如果您无权更改数据库架构,则可以编写查询以获取该信息

Select count(*) From tblindividualproduct where ProductID = @ProductIdText

检查具有该特定ID的行是否已存在的函数,仅当此函数返回false时才插入。 connString将是您的数据库连接字符串。

static public bool HasRecord(string ProductID, string connString)
{
    //add try catches where required
    bool foundRecord = true;
    Int32 numRecords = 0;
    string sql =
        "Select count(*) From tblindividualproduct where ProductID = @ProductIdText";
    using (SqlConnection conn = new SqlConnection(connString))
    {
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.Add("@ProductIdText", SqlDbType.VarChar);
        cmd.Parameters["@ProductIdText"].Value = ProductID;
        try
        {
            conn.Open();
            numRecords = (Int32)cmd.ExecuteScalar();
            if(numRecords == 0) foundRecord = false;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
    return foundRecord;
}

然后在您的插入代码

的函数中使用它
if(HasRecord("1","Data Source=192.x.x.x;Initial Catalog=mydbName;UserID=user;Password=passw;provider=SQLOLEDB") == false)
{
      //your insert code
}

http://www.dotnetperls.com/sqlclient

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar(v=vs.110).aspx