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;
}
我使用此代码在我的数据集中添加项目,你能帮助我检查输入,如果数据集上有重复的条目,你会不会接受它吗?
编辑:我已更新我的代码,以向您展示我目前的工作。
答案 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
}