Sql UPDATE 命令无效并且仅在网页中不会引发错误,控制台项目中的相同代码有效

发布于 2024-09-30 17:46:17 字数 2173 浏览 4 评论 0原文

我有一个 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁杀 2024-10-07 17:46:17

有一些想法可以帮助调试这个问题。

  1. 在 VtelCenter 表上查找可能会更改您的预期结果的任何 UPDATE 触发器(AFTER 或 INSTEAD OF)。
  2. 在数据库服务器上运行 SQL Profiler 跟踪,以便您可以捕获在该端传入的查询。

A couple of thoughts to help with debugging this.

  1. Look for any UPDATE triggers (either AFTER or INSTEAD OF) on the VtelCenter table that might be changing your expected results.
  2. Run a SQL Profiler trace on your database server so you can capture the query being passed in on that side.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文