C# Firebird FbDataReader 对象引用未设置为对象的实例

问题描述 投票:0回答:1

我在特定用户下从 Task Scheduler 启动一个 Windows 窗体程序,仅用于此目的。我对 FbDataReader 执行的数据库有一些查询,所有查询都执行正常,除了有时(不总是,这是最令人困惑的部分)。我收到错误 “System.NullReferenceException:'对象引用未设置到对象的实例。'”。我也尝试过不使用手动打开和关闭连接的语句,但仍然是同样的问题(我不知道使用语句或手动打开和关闭连接哪个更好?)。

错误发生在这个查询:

using (FbConnection ConnBMC = new FbConnection(BMC.connStringBMC))
{
    ConnBMC.Open();
    string ukaz = "SELECT IME,VREDNOST_STR,VREDNOST_REAL,VREDNOST_INT FROM PARAM WHERE IME = 'EPOSTA_UPORABNISKO_IME' OR IME = 'EPOSTA_GESLO' OR IME = 'EPOSTA_STREZNIK' OR IME = 'EPOSTA_VRATA' OR IME = 'EPOSTA_SEND_FROM_NOREPLY' OR IME = 'EPOSTA_NASLOV_INT'";

    using (FbCommand readCommand3 = new FbCommand(ukaz, ConnBMC))
    {
        using (FbDataReader myreader3 = readCommand3.ExecuteReader())
        {
            while (myreader3.Read())
            {
                if (myreader3["IME"].ToString().Trim() == "EPOSTA_UPORABNISKO_IME") BMC.emailUser = myreader3["VREDNOST_STR"].ToString().Trim();
                else if (myreader3["IME"].ToString().Trim() == "EPOSTA_GESLO") BMC.emailPass = myreader3["VREDNOST_STR"].ToString().Trim();
                else if (myreader3["IME"].ToString().Trim() == "EPOSTA_STREZNIK") BMC.emailServer = myreader3["VREDNOST_STR"].ToString().Trim();
                else if (myreader3["IME"].ToString().Trim() == "EPOSTA_VRATA") BMC.emailPort = myreader3["VREDNOST_STR"].ToString().Trim();
                else if (myreader3["IME"].ToString().Trim() == "EPOSTA_ENABLE_SSL") BMC.emailSSL = Convert.ToBoolean(myreader3["VREDNOST_INT"]);
                else if (myreader3["IME"].ToString().Trim() == "EPOSTA_NASLOV_INT") BMC.emailInternalAddress = myreader3["VREDNOST_STR"].ToString().Trim();
            }
        }
    }
}

如果我在初始化“readCommand3”(附件中的图像)后设置断点,一切看起来都很好,连接打开并且设置了有效的命令文本等等。所以,我真的不知道为什么会发生这种情况,为什么只发生几次。在那个查询之前,我在同一个数据库上有 9 个查询完美执行。

我正在使用 FirebirdSql.Data.FirebirdClient 版本 5.12.1

Breakpoint result Error message with commented if statements

堆栈跟踪:

   at FirebirdSql.Data.Client.Managed.Version10.GdsTransaction.BeginTransaction(TransactionParameterBuffer tpb)
   at FirebirdSql.Data.Client.Managed.Version10.GdsDatabase.BeginTransaction(TransactionParameterBuffer tpb)
   at FirebirdSql.Data.FirebirdClient.FbTransaction.BeginTransaction()
   at FirebirdSql.Data.FirebirdClient.FbCommand.Prepare(Boolean returnsSet)
   at FirebirdSql.Data.FirebirdClient.FbCommand.ExecuteCommand(CommandBehavior behavior, Boolean returnsSet)
   at FirebirdSql.Data.FirebirdClient.FbCommand.ExecuteReader(CommandBehavior behavior)
   at FirebirdSql.Data.FirebirdClient.FbCommand.ExecuteReader()
   at BMC.Form1.readParam() in C:\Development\Visual Studio 2017\Projects\BMC\BMC\Form1.cs:line 1008
   at BMC.Form1.Form1_Load(Object sender, EventArgs e) in C:\Development\Visual Studio 2017\Projects\BMC\BMC\Form1.cs:line 103
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at DevExpress.XtraEditors.XtraForm.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at DevExpress.XtraEditors.XtraForm.WndProc(Message& msg)
   at BMC.Form1.WndProc(Message& msg) in C:\Development\Visual Studio 2017\Projects\BMC\BMC\Form1.cs:line 455
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

我从 Firebird 数据库读取数据并得到空引用异常。

c# firebird firebird2.5 firebird-.net-provider
1个回答
0
投票

问题已通过升级到 FirebirdSql.Data.FirebirdClient 版本 9.1.1 解决。另见 A 的评论。塞拉克

我将 FirebirdSql.Data.FirebirdClient 更新为 9.1.1,现在出现错误 离开了。这只是对这种奇怪行为的合乎逻辑的解释。

© www.soinside.com 2019 - 2024. All rights reserved.