尝试使用 Oracle Data Access ODP.NET 11.2 执行 ExecuteReader() 时出现 InvalidOperationException

发布于 2024-10-25 02:18:58 字数 1839 浏览 11 评论 0原文

这是我第一次与 Oracle 合作,因为我们都讨厌与国外的东西一起工作,而你多年来一直在使用特定的模型,尽管这是我们的工作,我们必须做到这一点。

现在我已经安装了 Oracle 11 g 并复制并引用了 Oracle.DataAccess.dll 创建了一个方法,在该方法中打开连接并尝试从服务器上创建的视图中检索一些对象。

方法:

public BindingList<HeaderReceiver> GetHeaderReceivers()
{
    try
    {
        using (OracleConnection db = new OracleConnection(BaseDataAccess.ConnString))
        {
            string cmdText = "select * from p_customer t";
            BindingList<HeaderReceiver> headerReceivers = new BindingList<HeaderReceiver>();

            OracleCommand cmd = new OracleCommand(cmdText) { CommandType = CommandType.Text };
            db.Open();

            OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); <--- Error Occurs HERE!!!

            while (reader.Read())
                headerReceivers.Add(HeaderReceiver.GetInstance(reader));

            CustBranchRepository rep = new CustBranchRepository();
            headerReceivers.ForEach(p => p.DetailsBranch = rep.GetDetailReceivers(p.Id));

            reader.Close();
            db.Close();
            return headerReceivers;
        }
    }
    catch (Exception ex)
    {
        ExporterLogger.Log(ex);
        return null;
    }
}

现在,当 ExecuteReader() 提交时,我收到此 InvalidOperationException。

Operation is not valid due to the current state of the object.

StackTrace:

   at Oracle.DataAccess.Client.OracleCommand.ExecuteReader(Boolean requery, Boolean fillRequest, CommandBehavior behavior)
   at Oracle.DataAccess.Client.OracleCommand.ExecuteReader(CommandBehavior behavior)
   at Exporter.Boss.DataAccess.CustomerRepository.GetHeaderReceivers() in ...Exporter\Exporter.Boss.DataAccess\CustomerRepository.cs:line 25

任何想法和帮助...

谢谢!

It's the first time I HAVE to work with Oracle, and as we all hate to work with foreign stuff while you are working with a specific model some years, although this is our job and we have to make it.

Now i have installed the Oracle 11 g and copied and referenced the Oracle.DataAccess.dll created a method where opens a connection and tries to retrieve some objects from a view that has been created on the server.

Method:

public BindingList<HeaderReceiver> GetHeaderReceivers()
{
    try
    {
        using (OracleConnection db = new OracleConnection(BaseDataAccess.ConnString))
        {
            string cmdText = "select * from p_customer t";
            BindingList<HeaderReceiver> headerReceivers = new BindingList<HeaderReceiver>();

            OracleCommand cmd = new OracleCommand(cmdText) { CommandType = CommandType.Text };
            db.Open();

            OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection); <--- Error Occurs HERE!!!

            while (reader.Read())
                headerReceivers.Add(HeaderReceiver.GetInstance(reader));

            CustBranchRepository rep = new CustBranchRepository();
            headerReceivers.ForEach(p => p.DetailsBranch = rep.GetDetailReceivers(p.Id));

            reader.Close();
            db.Close();
            return headerReceivers;
        }
    }
    catch (Exception ex)
    {
        ExporterLogger.Log(ex);
        return null;
    }
}

Now when ExecuteReader() commits I get this InvalidOperationException.

Operation is not valid due to the current state of the object.

The StackTrace:

   at Oracle.DataAccess.Client.OracleCommand.ExecuteReader(Boolean requery, Boolean fillRequest, CommandBehavior behavior)
   at Oracle.DataAccess.Client.OracleCommand.ExecuteReader(CommandBehavior behavior)
   at Exporter.Boss.DataAccess.CustomerRepository.GetHeaderReceivers() in ...Exporter\Exporter.Boss.DataAccess\CustomerRepository.cs:line 25

Any thoughts and help...

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

伴梦长久 2024-11-01 02:18:58

看来您缺少命令对象上的连接......

db.Open();
OracleCommand cmd = new OracleCommand(cmdText) { CommandType = CommandType.Text };
cmd.Connection = db;

It looks like you're missing the connection on the command object...

db.Open();
OracleCommand cmd = new OracleCommand(cmdText) { CommandType = CommandType.Text };
cmd.Connection = db;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文