【问题标题】:Check for existing entry in data table检查数据表中的现有条目
【发布时间】:2014-04-21 02:17:27
【问题描述】:
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;
        }

我使用此代码在我的数据集中添加项目,您能帮我了解如何检查条目,如果它在数据集中有重复条目,则不会接受它?

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

【问题讨论】:

  • 重复条目是什么意思?您的意思是查询中的相同行?
  • 我的意思是如果我输入的产品 ID 为 1,我会再次输入。它不会接受它,因为它会在我的数据表中有重复的条目。

标签: c# mysql database datatable


【解决方案1】:

解决这个问题的实际方法是将您的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

【讨论】:

  • 我想做一个收银系统。我有不同的产品 ID。当我扫描条形码时,我将在我的网格视图中将其删除,因此我想定位我的数据表中是否有相同的条目..
  • 这就是它检查是否有该 id 的条目.. 你读过我的答案了吗?
  • 我遇到了一个错误。运算符 == 不能应用于 string 和 bool 类型的操作数
  • 这是该行中的确切错误 if(HasRecord("1","Data Source=192.xxx;Initial Catalog=mydbName;UserID=user;Password=passw;provider=SQLOLEDB" = =假)
  • @MixAustria 我已经更正了我错过了一个).. if(HasRecord("1","Data Source=192.xxx;Initial Catalog=mydbName;UserID=user;Password=passw;provider =SQLOLEDB") == false)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
  • 2014-07-15
  • 2020-12-14
  • 2017-04-02
相关资源
最近更新 更多