【发布时间】:2015-06-15 23:43:45
【问题描述】:
我遇到了一个问题,将连接从 TableAdapter 传递到某些方法会引发异常,指出 connectionstring 未初始化。除了这个例外,关于 SO 有很多问题,但没有一个通过连接,大多数是因为 ConnectionString 为空。奇怪的是我在整个方法链中都使用了MessageBox.Show(connection.ConnectionString);,并且我在每一步都收到了一个有效的连接字符串。这是一个有点复杂的程序,正在生产中,但我会尽量简化这个问题的代码......
这是 postInventoryData 方法,它采用带有库存项目的 DataGridView 并通过它迭代将它们发布到库存。我使用 TransactionScope 来确保在发生错误时安全回滚更改。如果一个项目是一个工具包(一个由其他项目组成的项目),我必须遍历这些项目并将它们从库存中删除。当我检查该项目是否为套件时会出现问题。
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 语句的所有工具包项目。我没有处于紧急模式或任何其他状态,但我认为这很奇怪,是我学习的机会。提前感谢您的帮助!
【问题讨论】:
-
您是否尝试过将字符串硬编码为测试?
-
感谢您进行编辑以清理我的代码顺便说一句...我没有尝试过,因为我想要/需要来自表适配器的连接。原因是我正在使用 TransactionScope,它要求范围内的所有函数都使用相同的连接,而不仅仅是相同的连接字符串。
-
再次查看后,我认为问题出在 getKit 方法的 using(myConnection){} 块中......在命令执行后,连接被收集和处理。我将在一秒钟内对此进行测试以确认这一点。大家觉得呢?
标签: c# connection-string sqlconnection tableadapter