AseDataReader 抛出 System.NullReferenceException

发布于 2024-09-14 16:06:04 字数 1144 浏览 4 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(5

就是爱搞怪 2024-09-21 16:06:04

我猜想 spCommand.ExecuteReader() 不是返回空读取器而是返回 null 对象。在这种情况下,我建议在阅读之前检查它是否为空:

AseDataReader reader = spCommand.ExecuteReader();
if(reader != null)
   while (reader.Read())
   {
        if (reader["MyColumn"] != DBNull.Value)
            valueList.Add(reader["MyColumn"].ToString());
   }

I guess that spCommand.ExecuteReader() instead of returning empty reader returns null object. In this case I suggest to chceck if it's null before reading:

AseDataReader reader = spCommand.ExecuteReader();
if(reader != null)
   while (reader.Read())
   {
        if (reader["MyColumn"] != DBNull.Value)
            valueList.Add(reader["MyColumn"].ToString());
   }
只是在用心讲痛 2024-09-21 16:06:04

这当然是因为 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

类似的东西

reader.ExecuteReader(CommandBehavior.CloseConnection)

或者我认为 Ł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

reader.ExecuteReader(CommandBehavior.CloseConnection)

Or i think ŁukaszW.pl's answer will suffice ;)

凡尘雨 2024-09-21 16:06:04

这有点盲目,但我会尝试使用Using来确定对象的范围:

using (AseCommand spCommand = new AseCommand())
{
     //etc..

     using (AseDataReader reader = spCommand.ExecuteReader())
     {
         // until end of while loop
     }
}

由于奇怪的行为,我们总是需要确定sybase对象的范围,特别是连接对象。无论如何,我认为 sybase 库不受管理,因此尽可能显式清理确实是最佳实践。

This is a bit of a shot in the dark, but I would try scoping the objects with Using:

using (AseCommand spCommand = new AseCommand())
{
     //etc..

     using (AseDataReader reader = spCommand.ExecuteReader())
     {
         // until end of while loop
     }
}

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.

一个人的旅程 2024-09-21 16:06:04

非常感谢您的回答。

但从 spCommand.ExecuteReader() 返回的 reader 不是 null。正如您从调用堆栈中看到的那样,异常发生在 Sybase.Data.AseClient1.AseDataReader.Read() 内部。

以下是我从反射器获得的代码。

public bool Read()
{
  this.OnTraceEnterEvent("Read", null);
  if (this._disposed)
  {
    throw new NullReferenceException();
  }
  if (this.IsClosed)
  {
    throw new AseException(DriverMsgNumber.ERR_RESULTSET_DEAD, null);
  }
  bool state = false;
  if (!this._bNoResultSet)
  {
    state = this.Peform_Next();
    state = this.CheckSingleRow(state);
  }
  this.OnTraceExitEvent("Read", state);
  return state;
}

我可以看到当 _disposetrue 时,可能会抛出一个 NullReferenceException。但不太确定这是否是在我的情况下抛出的异常。

Thanks a lot for the answers.

But reader returned from spCommand.ExecuteReader() is not null. The exception happens inside Sybase.Data.AseClient1.AseDataReader.Read() as you can see from the call stack.

Following is the code I got from reflector.

public bool Read()
{
  this.OnTraceEnterEvent("Read", null);
  if (this._disposed)
  {
    throw new NullReferenceException();
  }
  if (this.IsClosed)
  {
    throw new AseException(DriverMsgNumber.ERR_RESULTSET_DEAD, null);
  }
  bool state = false;
  if (!this._bNoResultSet)
  {
    state = this.Peform_Next();
    state = this.CheckSingleRow(state);
  }
  this.OnTraceExitEvent("Read", state);
  return state;
}

I can see one NullReferenceException could be thrown with _disposed as true. But not quite sure if it's the exception getting thrown in my situation.

瑶笙 2024-09-21 16:06:04

这是一个老问题,但自从我偶然发现这个问题并找到原因后,这​​就是:
我的阅读器不为空,但它有“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文