C# 找出导致 Sql 异常的列
我从 SQL 数据读取器(MS SQL 作为数据存储)收到异常,我想知道哪个列名导致抛出此异常。但我在 InnerException.. 中找不到它。
((System.InvalidOperationException)ex.InnerException).StackTrace:
System.Data.SqlClient.SqlDataReader.ReadColumnHeader(Int32 i)
System.Data.SqlClient.SqlDataReader.IsDBNull(Int32 i)
...
请问它藏在哪里?
I'm getting an exception from SQL Data Reader (MS SQL as datastore) and I'd like to know which column name causes this Exception to be thrown. But I cannot find it in the InnerException.. nowhere.
((System.InvalidOperationException)ex.InnerException).StackTrace:
System.Data.SqlClient.SqlDataReader.ReadColumnHeader(Int32 i)
System.Data.SqlClient.SqlDataReader.IsDBNull(Int32 i)
...
Where is it hidden please ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不能。数据读取器不会在其堆栈跟踪中传达该信息。您可以做的是将数据读取器的使用包装在另一个类中。在我正在进行的一个项目中,我们为此使用了扩展方法。该类如下所示:
您可以按如下方式使用它:
顺便说一句。这样的类还允许您获取
Nullable
对象,并摆脱那些您需要执行的手动DbNull
转换。You can't. The data reader does not communicate that in its stack trace. What you can do is wrapping the use of the data reader in another class. In a project I'm working on we used extension methods for this. The class looks like this:
You can use it as follows:
btw. Such an class also allows you to get
Nullable<T>
objects back and get rid of those manualDbNull
conversions you else need to do.