【发布时间】:2012-09-06 14:22:04
【问题描述】:
我在包装了这个 SQL 的 TableAdapter 中有一个查询(Oracle SQL,使用 ':' 而不是 '@' 作为参数化变量),我称之为 GetModel()
SELECT MFG_PRODUCT FROM FRAME_NO
WHERE FRM_NO = :FRM_NO
我预计查询可能会返回空值,因此我将列 MFG_PRODUCT 空值设置为 Null,正如 Microsoft 的本指南所指出的那样
http://msdn.microsoft.com/en-us/library/ya91ataz(v=vs.100).aspx
但是,当查询没有返回任何行时,我的应用程序仍然会抛出异常。我该怎么办?
我已经重建了整个解决方案,以防代码仍然引用旧的编译数据集,但仍然没有运气。
已编辑:这是代码。返回值为null时抛出异常
try
{
String modelCode = frameAdapter.GetModelCode(txtStdFrameId.Text.Trim()).ToString();
if(String.IsNullOrEmpty(modelCode)) //Go directly to exception when data is null here
{
//Process the data
}
}
catch(Exception ex)
{
//Log error to a text file
}
【问题讨论】:
-
抛出了什么异常?例外情况中包含哪些详细信息?
-
如何处理返回的行?仅当数据没有行时才会发生这种情况
-
是的,只有当返回的数据没有行时才会发生。例外是
System.NullReferenceException: Object reference not set to an instance of an object. -
那行
if(String.IsNullOrEmpty(modelCode))不能抛出异常。 -
在你的代码中放置一个断点来知道异常到底是在哪里抛出的——它可能是你的
frameAdapter.GetModelCode()