我有一个快速的问题,我的连接没有初始化。
当我调用create方法时,我传入了一堆varnames,比如“ABC,XYZ等”。
在foreach的第一次传递中,正确调用存储过程并且工作正常。但在第二遍,我得到一个“Connectionstring属性尚未初始化。
我应该在循环之外移动myConn.open和myConn.close吗?
我的代码:
public bool Create()
{
bool isSuccess = true;
ConnectionStringSettings myConnectionStringSetting = ConfigurationManager.ConnectionStrings[0];
DbProviderFactory dbFactory = DbProviderFactories.GetFactory(myConnectionStringSetting.ProviderName);
DbConnection myConn = dbFactory.CreateConnection();
myConn.ConnectionString = myConnectionStringSetting.ConnectionString;
DataTable myDataTable = new DataTable();
string[] varNamesArray = m_variableNames.Split(',');
foreach (string varName in varNamesArray)
{
string vn = varName.Trim();
//stored procedure
DbCommand myCommand = dbFactory.CreateCommand();
myCommand.CommandText = "s_LockVariables";
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Connection = myConn;
//variables
DbParameter param1 = myCommand.CreateParameter();
param1.ParameterName = "@varName";
param1.DbType = DbType.String;
param1.Value = vn;
myCommand.Parameters.Add(param1);
DbParameter param2 = myCommand.CreateParameter();
param2.ParameterName = "@datasetID";
param2.DbType = DbType.Int32;
param2.Value = m_datasetID;
myCommand.Parameters.Add(param2);
DbParameter param3 = myCommand.CreateParameter();
param3.ParameterName = "@userID";
param3.DbType = DbType.Int32;
param3.Value = m_userID;
myCommand.Parameters.Add(param3);
DbParameter param4 = myCommand.CreateParameter();
param4.ParameterName = "@currentTime";
param4.DbType = DbType.DateTime;
param4.Value = DateTime.Now;
myCommand.Parameters.Add(param4);
try
{
using (myConn)
{
myConn.Open();
myCommand.ExecuteNonQuery();
myConn.Close();
}
}
catch (Exception e)
{
isSuccess = false;
throw new Exception(e.Message);
//TODO: catch data error
return isSuccess;
}
}
return isSuccess;
}
答案 0 :(得分:6)
您在myConn
循环中处置foreach
:
using (myConn)
{
myConn.Open();
myCommand.ExecuteNonQuery();
myConn.Close();
}
我建议您改变它以在那里声明变量:
using (DbConnection myConn = dbFactory.CreateConnection())
{
myConn.ConnectionString = myConnectionStringSetting.ConnectionString;
myConn.Open();
myCommand.Connection = myConn;
myCommand.ExecuteNonQuery();
// Closed automatically due to being disposed
}
或者在循环外只有一个using
语句 - 你也可以为所有命令使用相同的连接,对吗?
答案 1 :(得分:2)
此代码
using (myConn)
{
myConn.Open();
myCommand.ExecuteNonQuery();
myConn.Close();
}
在第一个循环结束时销毁myConn var 我将尝试以这种方式改变它
using(DbConnection myConn = dbFactory.CreateConnection())
{
myConn.ConnectionString = myConnectionStringSetting.ConnectionString;
DataTable myDataTable = new DataTable();
string[] varNamesArray = m_variableNames.Split(',');
foreach (string varName in varNamesArray)
{
...
// the rest of your code
...
// Then do not add the using statement here
myConn.Open();
myCommand.ExecuteNonQuery();
myConn.Close();
....
}
}
另外,我将更改代码的try / catch部分 如果你真的需要从这个代码返回一个布尔值,那么
catch (Exception e)
{
isSuccess = false;
// If you rethrow a new Exception here you will never reach the return statement
// In this cases (assuming we are inside a library) I will try to save the exception
// in a global var (GetLastError comes to mind) or log everything in a file
return isSuccess;
}