属性尚未初始化.Winforms C#.net.



我正在向已经建立的Winforms应用程序添加新表单。

我的窗体上有一个 DataGridView,代码后面有一个相关方法,它调用我的 dbAPI DataTable 方法。我用与 dbAPI 类中使用的许多其他方法完全相同的代码编写了该方法,但由于某种原因它没有初始化连接字符串......

 public DataTable getMyTable()
    {
        //used for populating the DataGridView
        SqlCommand _com = new SqlCommand(string.Format("select * from tab.myTable where Country = 'Angola' "), _conn);
        _com.CommandTimeout = _command_timeout;
        DataSet _ds = new DataSet();
        SqlDataAdapter _adapt = new SqlDataAdapter();
        try
        {
            _adapt.SelectCommand = _com;
            int i = _adapt.Fill(_ds, "Asset_Transactions");
            if (_ds.Tables.Count > 0)
            {
                return _ds.Tables[0];
            }
            else
            {
                return makeErrorTable("GetMyTable", "No Table Returned for myTable");
            }
        }
        catch (Exception e)
        {
            return makeErrorTable("GetMyTable", e.Message);
        }
    }

_conn是一个 SQLConnection 对象。我的连接字符串在应用程序配置中...

  class dbAPI
{
    Utils _utils = new Utils();
    //this is the API between the Application Code and the LDB Database
    string _ldb_connection_string = (string)dii.Properties.Settings.Default.connLDB; //connection string but with only one  in settings as it gets converted to \
    int _command_timeout = Convert.ToInt32((string)dii.Properties.Settings.Default.commandTimeOut); //Command time out
    SqlConnection _conn = new SqlConnection();
    public dbAPI()
    {
        //constructor
    }
    #region --------------- Database Connectivity Section
    public string openLocalDatabaseConnection()
    {
        try
        {
            //try to create the connection
            _conn = new SqlConnection(_ldb_connection_string);
            _conn.Open();
        }
        catch (Exception e)
        {
            return string.Concat("Can't connect to LDB with '", e.Message, "'");
        }
        return ""; //success
    }
    public string closeLocalDatabaseConnection()
    {
        _conn.Close();
        _conn.Dispose();
        return "";
    }

我收到一个空的连接字符串,并且引发"连接字符串属性尚未初始化"异常。我不明白,因为我在课堂上还有许多其他方法可以毫无问题地工作。有什么想法吗?

谢谢

在继续操作之前,必须调用 openLocalDatabaseConnection() 函数,以便使用 Connection String 正确启动 SqlConnection 对象。

法典:

        public DataTable getMyTable()
        {           
            openLocalDatabaseConnection();
            //used for populating the DataGridView
            SqlCommand _com = new SqlCommand(string.Format("select * from tab.myTable where Country = 'Angola' "), _conn);
            _com.CommandTimeout = _command_timeout;
            DataSet _ds = new DataSet();
            SqlDataAdapter _adapt = new SqlDataAdapter();
            try
            {
                _adapt.SelectCommand = _com;
                int i = _adapt.Fill(_ds, "Asset_Transactions");
                if (_ds.Tables.Count > 0)
                {
                    return _ds.Tables[0];
                }
                else
                {
                    return makeErrorTable("GetMyTable", "No Table Returned for myTable");
                }
            }
            catch (Exception e)
            {
                return makeErrorTable("GetMyTable", e.Message);
            }
        }
例如,

将应用程序配置文件添加到您的项目中(或您拥有的项目内部)并放置它(当然,您放置的名称和连接字符串):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name ="MyConnection" connectionString ="your connection string here"/>
  </connectionStrings>
</configuration>

然后在您的_ldb_connection_string中:

string _ldb_connection_string = ConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;

最新更新