连接后获取数据但是立即连接对象变为空

时间:2016-09-01 10:00:06

标签: c# mysql sql-server sqlconnection

我正在使用Windows应用程序,这就是我用来连接服务器和检索数据的方式。

我动态设置此凭据..

 Global.sqlConn = new SQLDB(Internet.gIpAddress, Internet.gCatalog, Internet.gOnlineDbUser, Internet.gOnlineDbPwd);
 dtConsignmentInfo = Global.sqlConn.ExecuteSPDataTable("ro_GetConsignmentInformation", para);

       try
          {

           if (Global.sqlConn.DBException == null)
                {
                    if (Global.sqlConn.ErrorMessage == "")
                         {
                            if (dtConsignmentInfo != null && dtConsignmentInfo.Rows.Count > 0)
                                {
                                    //somecode   
                                    Global.sqlConn = null;// Clean Up
                                }
                                else
                                {
                                  //somecode   
                                  Global.sqlConn = null;// Clean Up
                                }
                      }
                 }

类文件SQLDB:

 public SQLDB(string mServerName, string mDBName, string mUserID, string mPassword)
        {
            cnn = new SqlConnection();

            cnn.ConnectionString = "Data Source=" + mServerName + ";Initial Catalog=" + mDBName + ";Persist Security Info=True;" + "User ID=" + mUserID + ";Password=" + mPassword + ";Application Name='Regional Office Software';";
        }

SQLDB中的方法

 public DataTable ExecuteSPDataTable(string spName, List<SqlParameter> param = null)
    {
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();

        // Initialize the Exception & Error Message...
        this.ErrorMessage = "";
        this.DBException = null;

        try
        {
            // Prepare DataAdapter ...
            da.SelectCommand = new SqlCommand();
            da.SelectCommand.Connection = cnn;
            da.SelectCommand.CommandText = spName;
            da.SelectCommand.CommandType = CommandType.StoredProcedure;

            // Pass Parameters ...
            if (param != null)
            {
                da.SelectCommand.Parameters.AddRange(param.ToArray());
            }

            // Open Database Connection ...
            cnn.Open();

            // Fill the DataTable ...
            da.Fill(dt);
        }
        catch (SqlException exception)
        {
            this.DBException = exception;
            dt = null;
        }
        catch (Exception exception)
        {
            // Set the Exception ...
            this.DBException = exception;
            dt = null;
        }
        finally
        {
            if (cnn.State == ConnectionState.Open)
                cnn.Close();
        }

        return dt;
    }

但问题是在执行此行dtConsignmentInfo = Global.sqlConn.ExecuteSPDataTable("ro_GetConsignmentInformation", para);之后 我在dtConsignmentInfoif (Global.sqlConn.DBException == null)获取数据,此行我得到对象引用错误sqlConn变为空

主要的事情我每次都会得到这个错误如何解决这个问题?

0 个答案:

没有答案