AseDataReader 抛出 System.NullReferenceException
我正在使用 Sybase.AdoNet2.AseClient 从控制台 C# 应用程序访问 Sybase ASE 数据。并非总是如此,但有时我会得到 System.NullReferenceException 使用以下代码。
它在仅启动一个应用程序的情况下运行良好,但如果我在计算机中同时启动 10 个进程,则会出现此异常。
public void Dummy()
{
List<string> valueList = new List<string>();
AseParameter[] arParms = new AseParameter[1];
arParms[0] = new AseParameter("@date", AseDbType.Date);
arParms[0].Value = Convert.ToDateTime("1/08/2010");
AseCommand spCommand = new AseCommand();
spCommand.CommandType = CommandType.StoredProcedure;
spCommand.Connection = connection;
spCommand.CommandText = "MyStoredProcedure";
spCommand.Parameters.AddRange(arParms);
AseDataReader reader = spCommand.ExecuteReader();
while (reader.Read())
{
if (reader["MyColumn"] != DBNull.Value)
valueList.Add(reader["MyColumn"].ToString());
}
}
它发生在“while (reader.Read())”行中,并具有以下调用堆栈。
System.NullReferenceException:未将对象引用设置为对象的实例。 在 Sybase.Data.AseClient1.AseDataReader.Read()
在 Sybase.Data.AseClient.AseDataReader.Read()
at Dummy()
如果有人能帮助我,我将不胜感激。
I’m using Sybase.AdoNet2.AseClient to access Sybase ASE data from a Console C# Application. Not always, but from time to time I get System.NullReferenceException With following code.
It works well with only one application started, but fails with this exception if I start 10 processes at the same time in my machine.
public void Dummy()
{
List<string> valueList = new List<string>();
AseParameter[] arParms = new AseParameter[1];
arParms[0] = new AseParameter("@date", AseDbType.Date);
arParms[0].Value = Convert.ToDateTime("1/08/2010");
AseCommand spCommand = new AseCommand();
spCommand.CommandType = CommandType.StoredProcedure;
spCommand.Connection = connection;
spCommand.CommandText = "MyStoredProcedure";
spCommand.Parameters.AddRange(arParms);
AseDataReader reader = spCommand.ExecuteReader();
while (reader.Read())
{
if (reader["MyColumn"] != DBNull.Value)
valueList.Add(reader["MyColumn"].ToString());
}
}
It happens in the line of “while (reader.Read())”, and has following call stack.
System.NullReferenceException: Object reference not set to an instance of an object.
at Sybase.Data.AseClient1.AseDataReader.Read()
at Sybase.Data.AseClient.AseDataReader.Read()
at Dummy()
Will be very appreciated if anybody can help me out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我猜想
spCommand.ExecuteReader()
不是返回空读取器而是返回null
对象。在这种情况下,我建议在阅读之前检查它是否为空:I guess that
spCommand.ExecuteReader()
instead of returning empty reader returnsnull
object. In this case I suggest to chceck if it's null before reading:这当然是因为 spCommand.ExecuteReader 返回 null。但不幸的是我无法找出为什么 SysBook Online 参考中抛出 Null 异常。也许尝试使用不同的命令行为
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.sdk_12.5.1.adonet/html/adonet/Asacommand_apiref.htm
http://msdn.microsoft.com/en-us/ library/system.data.commandbehavior.aspx
类似的东西
或者我认为 ŁukaszW.pl 的答案就足够了;)
Well this of course because spCommand.ExecuteReader returns a null. But unfortunately i couldn't find out why the Null Exception is thrown in the SysBook Online reference. Perhaps try with a different command behavior
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.sdk_12.5.1.adonet/html/adonet/Asacommand_apiref.htm
http://msdn.microsoft.com/en-us/library/system.data.commandbehavior.aspx
Something like
Or i think ŁukaszW.pl's answer will suffice ;)
这有点盲目,但我会尝试使用Using来确定对象的范围:
由于奇怪的行为,我们总是需要确定sybase对象的范围,特别是连接对象。无论如何,我认为 sybase 库不受管理,因此尽可能显式清理确实是最佳实践。
This is a bit of a shot in the dark, but I would try scoping the objects with Using:
We have always needed to scope our sybase objects, connection objects in particular, due to strange behaviors. In any case I don't think the sybase libs are managed, so it is really best practices to clean up explicitly wherever possible.
非常感谢您的回答。
但从
spCommand.ExecuteReader()
返回的reader
不是null
。正如您从调用堆栈中看到的那样,异常发生在Sybase.Data.AseClient1.AseDataReader.Read()
内部。以下是我从反射器获得的代码。
我可以看到当
_dispose
为true
时,可能会抛出一个NullReferenceException
。但不太确定这是否是在我的情况下抛出的异常。Thanks a lot for the answers.
But
reader
returned fromspCommand.ExecuteReader()
is notnull
. The exception happens insideSybase.Data.AseClient1.AseDataReader.Read()
as you can see from the call stack.Following is the code I got from reflector.
I can see one
NullReferenceException
could be thrown with_disposed
astrue
. But not quite sure if it's the exception getting thrown in my situation.这是一个老问题,但自从我偶然发现这个问题并找到原因后,这就是:
我的阅读器不为空,但它有“IsClosed = true”,因为连接在读取数据之前已关闭。
根据 http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.sdk_12.5.1.adonet/html/adonet/Islined_asadatareader_apiref.htm 一旦阅读器关闭,您只能调用 IsClosed 和 RecordsAffected。
This is an old question, but since I stumbled upon this problem and found the reason, here is it :
My reader wasn't null, but it had "IsClosed = true" because the connection was closed before reading the data.
According to http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.help.sdk_12.5.1.adonet/html/adonet/Isclosed_asadatareader_apiref.htm once the reader is closed, you can only call IsClosed and RecordsAffected.