Sql UPDATE 命令无效并且仅在网页中不会引发错误,控制台项目中的相同代码有效
我有一个 .NET 3.5 Web 应用程序,它有一组处理实体持久性的类。 INSERT 和 SELECT 准备好的命令有效。但是 UPDATE 命令永远不会起作用(没有更新数据库记录)并且它永远不会引发异常。而且它始终返回 1,因此即使 command.ExecuteNonQuery() 也会返回有效的受影响行数。
现在,当我采用相同的实体类并在测试控制台应用程序中运行它时,准备好的语句可以工作。
这确实令人沮丧,而且完全是一个阻碍。我什至在 Ubuntu、Mac OS X 和 Windows 上的 Mono 中尝试过这个。全部执行相同(网络应用程序中没有更新记录,插入有效,控制台应用程序有效)。
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();
}
I have a .NET 3.5 web application which has a set of classes handling entity persistence. The INSERT and SELECT prepared commands work. However the UPDATE command never works (no database record is updated) and it never throws an exception. Also it always returns 1, so even the command.ExecuteNonQuery() returns a valid number of affected rows.
Now when I take the same entity class and run it in a test console application, the prepared statement works.
This is really frustrating and a complete show stopper. I have even tried this in Mono on Ubuntu, Mac OS X and Windows. All perform the same (no records updated in web app, insert works, and console app works).
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一些想法可以帮助调试这个问题。
A couple of thoughts to help with debugging this.