从OdbcDataAdapter.Fill错误 [英] Error from OdbcDataAdapter.Fill

查看:273
本文介绍了从OdbcDataAdapter.Fill错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我执行OdbcDataAdapter.Fill(DataTable)时有时会得到两个不同的错误。
下面是代码示例:

I sometimes get two different of error when executing OdbcDataAdapter.Fill(DataTable). Here is the code example:

string odbc = "select item, upcno from table";
OdbcCommand cmd = new OdbcCommand(odbc, fconn);
OdbcDataAdapter oda = new OdbcDataAdapter(cmd);
oda.Fill(dt);




  1. System.NullReferenceException:对象引用未设置为实例

  1. System.NullReferenceException: Object reference not set to an instance of an object.

System.InvalidOperationException:对于行/列没有数据。

System.InvalidOperationException: No data exists for the row/column.

有人有解决这个问题的线索吗?

Does anyone have clue to resolve this problem?

推荐答案

从你的问题获取连接对象,但你可以做如下。不创建类级别连接,您可以在需要时创建它,并在最后正确处理它。

Not sure where you get connection object from your question but you can do as below. don't create class level connections, you can create it when you want and properly dispose it at the end.

public DataTable GetDataTableFromAdapter(string queryString)
{
    DataTable dt = new DataTable();
    using (OdbcConnection connection =
                new OdbcConnection(ConnectionString))
    {
        using (OdbcDataAdapter adapter =
                new OdbcDataAdapter(queryString, connection))
        {
            connection.Open();
            adapter.Fill(dt);
        }
    }
    return dt;
}

称为

DataTable dt = GetDataTableFromAdapter("select [item], [upcno] from [table]");

这篇关于从OdbcDataAdapter.Fill错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆