私は、UPCのタイトルと数量という名前の列を持つデータベースを持っています。他にも更新値がある場合は、Mysqlの値を確認してください。
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 " +
"([email protected]) 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();
私はちょうどよ:私は、UPC番号が既に列に存在しており、ITはその後、ここ1によって、そのエントリの数量を更新しない場合は、私がこれまで持っているものであるかどうかを確認するためにチェックを行うにしようとしていますそれをチェックするためにどのように語るべきかわからない。
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();
}
あなたが実際にこのために* *のアップサートを使用することができます - http://en.wikipedia.org/wiki/Upsert –
ので、もし私は挿入物またはuの代わりにupsertをするそれをpdateするか、どちらかを行うでしょうか? –
うわー、両方の文を1つのupsertに置き換えてください。ロジックは「存在しない場合はこれを挿入し、そうでない場合は行を更新します」。 –