我有一个数据库,其中一些列名为UPC标题描述和数量。我正在尝试检查列中是否已存在UPC编号,如果它然后将此条目的数量更新为1,那么我到目前为止:
MySqlConnection connection = new MySqlConnection(MyConString);
connection.Open();
MySqlCommand command = connection.CreateCommand();
command.CommandText = "INSERT INTO tableName " +
"(upc, title, description, quantity) " +
"VALUES " +
"(@upc, @title, @description, @quantity)";
MySqlCommand upcCheck = connection.CreateCommand();
upcCheck.CommandText = "Select COUNT (*) FROM tableName WHERE " +
"(upc=@upc) LIMIT 1";
upcCheck.Parameters.AddWithValue("@upc", upc.Text); //Checks for duplicate UPCs
MySqlDataReader check = upcCheck.ExecuteReader();
if( check.Read() != false)
{
command.Parameters.AddWithValue("@quantity",++); //not sure how to word it right
}
command.Parameters.AddWithValue("@upc", upc.Text); //adding parameters SAFELY to the statement
command.Parameters.AddWithValue("@title", titlename);
command.Parameters.AddWithValue("@description", descname);
command.Parameters.AddWithValue("@quantity", "1");
MySqlDataReader result = command.ExecuteReader();
我只是不确定如何用它来做检查。
UPDATE!这是工作代码。
MySqlConnection connection = new MySqlConnection(MyConString);
connection.Open();
MySqlCommand updatecommand = connection.CreateCommand();
updatecommand.CommandText = "UPDATE tableName Set quantity = quantity +1 " +
"WHERE upc = @upc";
updatecommand.Parameters.AddWithValue("@upc", upc.Text);
using (MySqlDataReader updateResult = updatecommand.ExecuteReader())
{
if (updateResult.RecordsAffected == 0)
{
affected = true;
}
else
{
affected = false;
}
}
if(affected == true)
{
MySqlCommand command = connection.CreateCommand();
command.CommandText = "INSERT INTO tableName " +
"(upc, title, description, quantity) " +
"VALUES " +
"(@upc, @title, @description, @quantity)";
command.Parameters.AddWithValue("@upc", upc.Text);
command.Parameters.AddWithValue("@title", titlename);//adding parameters SAFELY to the statement
command.Parameters.AddWithValue("@description", descname);
command.Parameters.AddWithValue("@quantity", "1");
MySqlDataReader result = command.ExecuteReader();
}
答案 0 :(得分:1)
如果UPC是主键,则可以使用MySQL数据库的INSERT ON DUPLICATE KEY UPDATE
功能。如果您不希望INSERT
新UPC
只是运行UPDATE
查询,如果没有这样的行,如果不会影响表格中的数据。
UPDATE <tablename> SET quantity=quantity+1 WHERE UPC='<upc>'
答案 1 :(得分:1)
虽然我没有专门使用MySqlDataReader,但我会稍微改变一下查询......否则你对插入的格式(参数化)就完全没了了。
虽然Hristo提供的ON DUPLICATE KEY UPDATE非常酷,但您将缺少记录的其他列元素。我会换到..
MySqlCommand command = connection.CreateCommand();
command.CommandText = "UPDATE tableName Set Quantity = Quantity +1 " +
"WHERE upc = @upc"
command.Parameters.AddWithValue("@upc", upc.Text);
**不知道如何从MySqlDataReader返回值/状态,但
result = command.ExecuteReader();
if( the status indicates it actually succeeded in an update,
or how many records were updated > 0 )
return; // you've just increased the counter...
另外,它没有存档...继续你的SQL-insert命令的其余部分,我们可以覆盖最后一个命令实例......
command = connection.CreateCommand();
command.CommandText = "INSERT INTO tableName " +
"(upc, title, description, quantity) " +
"VALUES " +
"(@upc, @title, @description, @quantity)";
command.Parameters.AddWithValue("@upc", upc.Text);
command.Parameters.AddWithValue("@title", titlename);
command.Parameters.AddWithValue("@description", descname);
command.Parameters.AddWithValue("@quantity", "1");
result = command.ExecuteReader();