从表适配器传递连接生成连接字符串属性尚未初始化

本文关键字:连接 字符串 属性 初始化 适配器 | 更新日期: 2023-09-27 17:55:25

我遇到了一个问题,将连接从 TableAdapter 传递到某些方法会引发异常,指出connectionstring未初始化。关于SO有很多问题,但没有人传递连接,大多数是因为ConnectionString为空。奇怪的是,我在整个方法链中使用了MessageBox.Show(connection.ConnectionString);,并且在每一步都收到一个有效的连接字符串。这是一个正在生产中的有点复杂的程序,但我会尝试简化这个问题的代码......

这是 postInventoryData 方法,该方法采用包含清单项的 DataGridView,并循环访问它,将它们发布到清单。我使用事务范围来确保在发生错误时安全地回滚更改。如果一个项目是工具包(由其他项目组成的项目),我必须遍历这些项目并将它们从库存中删除。当我检查该项目是否是套件时出现问题。

    public bool postInventoryData(DataGridView dgv)
    {
        bool successful = true;
        TestDataSetTableAdapters.inentoryTrxTableAdapter inventoryTrxAdapter = 
                        new TestDataSetTableAdapters.inentoryTrxTableAdapter();
        try
        {
            using (TransactionScope trxScope = new TransactionScope
                         (TransactionScopeOption.Required, new System.TimeSpan(0, 15, 0)))
            {
                MessageBox.Show(inventoryTrxAdapter.Connection.ConnectionString); // <-- Valid ConnectionString
                inventoryTrxAdapter.OpenConnection();
                for (int i = 0; i < dgv.Rows.Count; i++)
                {
                    //parameter values
                    string departmentCode = dgv.Rows[i].Cells["Department_Code"].Value.ToString();
                    string machineCode = dgv.Rows[i].Cells["Machine_Code"].Value.ToString();
                    string operatorCode = dgv.Rows[i].Cells["Operator_Code"].Value.ToString();
                    string itemNumber = dgv.Rows[i].Cells["Item_Number"].Value.ToString();
                    double? qtyProduced = Convert.ToDouble(dgv.Rows[i].Cells["Quantity"].Value.ToString());
                    bool isKit = 
                       businessLayer.isItemNumberKit
                       (inventoryTrxAdapter.Connection, itemNumber); // <-- CULPRIT!
                    // Inserts the item
                    dailyProductionInsertQty(
                        departmentCode,
                        machineCode,
                        operatorCode,
                        itemNumber,
                        isKit,
                        qtyProduced,
                        inventoryTrxAdapter,
                        trxScope);
                }
                inventoryTrxAdapter.CloseConnection();
                trxScope.Complete();
            }
        }
        catch (System.Exception ex)
        {
            successful = false;
            MessageBox.Show(ex.ToString());
        }
        return successful;
    }

isItemNumberKit 方法

    public bool isItemNumberKit(SqlConnection connection, string itemNumber)
    {
        bool contains;
        MessageBox.Show(connection.ConnectionString); // <-- Valid ConnectionString
        DataTable dt = getKit(connection, itemNumber); // <-- CULPRIT!
        if (dt.Rows.Count > 0)
        {
            contains = true;
        }
        else
        {
            contains = false;
        }
        return contains;
    }

getKit 方法

    public DataTable getKit(SqlConnection connection, string itemNumber)
    {
        DataTable dt = new DataTable();
        SqlConnection myConnection = connection;
        MessageBox.Show(myConnection.ConnectionString); // <-- Valid ConnectionString
        SqlParameter paramItemNumber = new SqlParameter();
        paramItemNumber.ParameterName = "@ItemNumber";
        paramItemNumber.Value = itemNumber;
        paramItemNumber.SqlDbType = System.Data.SqlDbType.VarChar;
        try
        {
            using (myConnection)
            {
                string sql =
                        @"SELECT kits.Row_Id, 
                          kits.Kit_Item_Number, 
                          kits.Location_Code        
                          FROM Inventory.dbo.Z_PV_Kits kits 
                          WHERE kits.Kit_Item_Number=@ItemNumber";
                //myConnection.Open();
                using (SqlCommand myCommand = new SqlCommand(sql, myConnection))
                {
                    myCommand.Parameters.Add(paramItemNumber);
                    SqlDataReader reader = myCommand.ExecuteReader();
                    dt.Load(reader);
                }
            }
        }
        catch (Exception ex)
        {
            dt = null;
            MessageBox.Show(ex.ToString());
        }
        return dt;
    }

当我执行postInventoryData程序抛出异常,并显示消息"连接字符串属性尚未初始化",行号指向isItemNumberKit和getKit。正如您在上面的代码中看到的,我在整个过程中以及每次收到有效的连接字符串时都使用了MessageBox.Show(connection.ConnectionString)。我创建了一个解决方法,它存储了一个缓存的 DataTable,其中包含我可以运行 linq 语句的所有工具包项。我没有处于紧急模式或任何东西,但我认为这很奇怪,是我学习的机会。提前感谢任何帮助!

从表适配器传递连接生成连接字符串属性尚未初始化

解决方案中可能有 2 个 app.config 文件,其中包含 2 个不同的连接字符串。

好吧,我想通了,现在当我考虑它时,答案有些明显。我总是使用 using(){} 块来确保连接和类似对象在使用后得到正确处理和处理。解决方案是简单地从getKit方法中删除using(myConnection){}块,如下所示:

    public DataTable getKit(SqlConnection connection, string itemNumber)
    {
        DataTable dt = new DataTable();
        SqlConnection myConnection = connection;
        MessageBox.Show(myConnection.ConnectionString);
        SqlParameter paramItemNumber = new SqlParameter();
        paramItemNumber.ParameterName = "@ItemNumber";
        paramItemNumber.Value = itemNumber;
        paramItemNumber.SqlDbType = System.Data.SqlDbType.VarChar;
        try
        {
            string sql =
@"SELECT    kits.Row_Id, 
        kits.Kit_Item_Number, 
        kits.Location_Code      
FROM    Inventory.dbo.Z_PV_Kits kits 
WHERE kits.Kit_Item_Number=@ItemNumber 
";
            //myConnection.Open();
            using (SqlCommand myCommand = new SqlCommand(sql, myConnection))
            {
                myCommand.Parameters.Add(paramItemNumber);
                SqlDataReader reader = myCommand.ExecuteReader();
                dt.Load(reader);
            }
        }
        catch (Exception ex)
        {
            dt = null;
            MessageBox.Show(ex.ToString());
        }
        return dt;
    }

这将使连接保持不变,但会正确释放命令。很抱歉这个冗长的问题,答案很简单。希望有一天这可能会帮助某人。