简体   繁体   English

如果Sql Server返回错误,则应抛出SqlException。 但这不是

[英]If Sql Server returns an error, then SqlException should be thrown. But it isn't


If I bind GridView to SqlDataSource and also set AutoGenerateEditButton to true, and if I then try to update a field ( this field being a primary key in database ), then database should return an error and thus SqlException should be thrown?! 如果我将GridView绑定到SqlDataSource并将AutoGenerateEditButton也设置为true,然后尝试更新一个字段(该字段是数据库中的主键),那么数据库应该返回错误,因此应该抛出SqlException? So why doesn't page report an exception? 那么,为什么页面不报告异常? Instead, all Gridview does is setting all fields in that row back to their original values. 相反,所有Gridview所做的就是将该行中的所有字段都设置回其原始值。


thanx 谢谢


EDIT: 编辑:

Hello, 你好,


When I executed same update statement with the following code, I got Cannot update identity column 'EmployeeID' exception , so I'm assuming Sql Server did report same error when GridView tried to update, but for some reason exception wasn't raised: 当我使用以下代码执行相同的更新语句时,出现无法更新标识列'EmployeeID'的异常 ,因此我假设GridView尝试更新时Sql Server确实报告了相同的错误,但由于某种原因未引发异常:

        SqlConnection sc = new SqlConnection();
        sc.ConnectionString = @"Data source=localhost; integrated security=sspi; initial catalog=northwind;";
        SqlCommand sComand = new SqlCommand();

        sComand.Connection = sc;
        sComand.CommandText = "update Employees set EmployeeId=100,FirstName='Suzy',"+ 
                   "LastName='Smile',City='Moon' where EmployeeId=1";
        sc.Open();
        int I = sComand.ExecuteNonQuery();


SECOND EDIT: 第二编辑:

Is your DataKeyNames property set correctly? 您的DataKeyNames属性设置正确吗?

I tried with setting DataKeyNames="EmployeeId", but exception was still not raised 我尝试设置DataKeyNames =“ EmployeeId”,但仍未引发异常

If your SQL query returns a result, then C# will get that result. 如果您的SQL查询返回结果,则C#将获得该结果。 If your SQL query throws an exception, then C# will get that exception. 如果您的SQL查询引发异常,则C#将获得该异常。 You cannot mix and match here, it's not a wardrobe. 您不能在这里混搭,这不是衣柜。

An exception will be thrown ONLY if the ERROR severity in T-SQL is 16 or higher AFAIK. 仅当T-SQL中的错误严重性为16或更高AFAIK时,才会引发异常。 So not all errors are the same in SQL Server. 因此,并非所有错误在SQL Server中都是相同的。

实际上,您应该能够无误地修改主键,只要它不会导致重复即可。

您的DataKeyNames属性设置正确吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Linq 到 Sql - 没有抛出 ChangeConflictException。 为什么? - Linq To Sql - ChangeConflictException not being thrown. Why? 应该是NullReferenceException并不总是抛出 - NullReferenceException isn't always thrown when it should be 抛出异常时不显示自定义错误页面 - Custom error page isn't shown when exception thrown 奇怪的行为-发生错误,但未引发异常 - Strange behavior - error happens but exception isn't thrown 尝试连接到SQL Server数据库时出现“ SqlException未处理”错误 - 'SqlException was unhandled' error when trying to connect to SQL Server database 使用linq插入批量数据时发生错误{引发了'System.OutOfMemoryException'类型的异常。} - Error occured while inserting bulk data by using linq {Exception of type 'System.OutOfMemoryException' was thrown.} 抛出异常:尝试简单插入 SQL 服务器数据库时 System.Data.dll 中的“System.Data.SqlClient.SqlException” - Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll when trying to make simple insertion into SQL Server database 使用SQL Server的应用程序中的SqlException - SqlException in app using SQL Server 将数据存储到SQL Server中的SqlException - SqlException on storing data into SQL Server 引发了类型为'System.StackOverflowException'的异常。 - Exception of type 'System.StackOverflowException' was thrown.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM