エンティティの永続性を処理する一連のクラスを持つ .NET 3.5 Web アプリケーションがあります。INSERT および SELECT 準備済みコマンドが機能します。ただし、UPDATE コマンドは機能せず (データベース レコードは更新されません)、例外もスローされません。また、常に 1 を返すため、command.ExecuteNonQuery() でさえ、影響を受ける有効な行数を返します。
同じエンティティ クラスをテスト コンソール アプリケーションで実行すると、プリペアド ステートメントが機能します。
これは本当にイライラし、完全なショー ストッパーです。Ubuntu、Mac OS X、および Windows の Mono でもこれを試しました。すべてが同じように実行されます (Web アプリでレコードが更新されず、挿入が機能し、コンソール アプリが機能します)。
public void Store()
{
SqlConnection conn = new SqlConnection(this.connection_string);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
int i = 0;
if (this.id == 0)
{
// INSERT a new RECORD
cmd.CommandText = "INSERT INTO [VtelCenter] ([CommonName],[Location]) VALUES (@commonname, " +
"@location)";
cmd.Parameters.Add("@commonname", SqlDbType.NVarChar, this.CommonName.Length);
cmd.Parameters["@commonname"].Value = this.CommonName;
cmd.Parameters.Add("@location", SqlDbType.NVarChar, this.Location.Length);
cmd.Parameters["@location"].Value = this.Location;
}
else
{
// UPDATE an existing RECORD
cmd.CommandText = "UPDATE [VtelCenter] SET [CommonName] = @commonname, [Location] = @location, " +
"[Status] = @status WHERE [ID] = @id";
//cmd.CommandText = "EXEC [dbo].[UpdateVtelCenter] @id, @commonname, @location, @status";
cmd.Parameters.Add("@commonname", SqlDbType.NVarChar, this.commonName.Length);
cmd.Parameters["@commonname"].Value = this.CommonName;
cmd.Parameters.Add("@location", SqlDbType.NVarChar, this.Location.Length);
cmd.Parameters["@location"].Value = this.Location;
cmd.Parameters.Add("@status", SqlDbType.Int);
cmd.Parameters["@status"].Value = (int) this.Status;
cmd.Parameters.Add("@id", SqlDbType.Int);
cmd.Parameters["@id"].Value = this.Id;
}
cmd.Prepare();
i = cmd.ExecuteNonQuery();
if (i != 1)
throw new Exception(string.Format("Incorrect number of records stored: {0}, should be 1.", i));
conn.Close();
}