0

我有一个快速的问题,我的连接没有被初始化。

当我调用 create 方法时,我传入了一堆 varname,比如“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;
    }
4

2 回答 2

6

你在这里myConnforeach循环中处理:

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语句- 您也可以对所有命令使用相同的连接,对吗?

于 2012-07-05T22:46:50.353 回答
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;       
}       
于 2012-07-05T22:48:36.283 回答