私は mysql データベースのレコードを更新するメソッドを持っています。次のように文字列を単に使用すると、クエリは完全にうまく機能します。
string query = @"UPDATE Batches SET `State`=@State, `PostDocCount`=@PostDocCount WHERE `FilePath` LIKE '%MYDIRECTORYSTRING%'";
ただし、MYDIRECTORYSTRING をパラメーター化されたクエリに置き換えると、次のようになります。
string query = @"UPDATE Batches SET `State`=@State, `PostDocCount`=@PostDocCount WHERE `FilePath` LIKE '%@dirName%'";
機能しなくなりました(レコードは更新されません)- @dirName == MYDIRECTORYSTRING も確認しました。だから私はこれが構文の問題であると信じさせられますか? (両方のクエリは正常に実行されるように見えますが..)
以下は私の完全な方法です:
public void UpdateBatch(string postDocCount, string dirName, string state)
{
string query = @"UPDATE Batches SET `State`=@State, `PostDocCount`=@PostDocCount WHERE `FilePath` LIKE '%@dirName%'";
//Open connection
if (this.OpenConnection() == true)
{
//create mysql command
MySqlCommand cmd = new MySqlCommand();
//Assign the query using CommandText
cmd.CommandText = query;
//Assign the connection using Connection
cmd.Connection = connection;
// Parametized queries
cmd.Parameters.AddWithValue("@PostDocCount", postDocCount);
cmd.Parameters.AddWithValue("@State", state);
cmd.Parameters.AddWithValue("@dirName", dirName);
//Execute query
cmd.ExecuteNonQuery();
//close connection
this.CloseConnection();
}
}