【问题标题】:'The ConnectionString property has not been initialized.' TO FIX'ConnectionString 属性尚未初始化。'修理
【发布时间】:2018-03-07 10:07:46
【问题描述】:

我目前收到错误: System.InvalidOperationException:“ConnectionString 属性尚未初始化。” 我想要一些帮助来解决这个问题,因为我正在尝试将数据从 c#form(多个文本框)输入到我当前代码的 SQL 数据库表单

 private void AcceptData()
    {
        using (Connection = new SqlConnection(connectionString))
        using (SqlDataAdapter adapter = new SqlDataAdapter("INPUT INTO Person", Connection))//, Connection
        {
            DataTable RegisterTable = new DataTable();
            adapter.Fill(RegisterTable); //System.InvalidOperationException: 'The ConnectionString property has not been initialized.' TO FIX

            string name = textBox1.Text;
            string organisation = textBox3.Text;
            DateTime Time = DateTime.Parse(textBox2.Text);
            string strDateTimeIn = Time.ToString("yyyy-MM-dd HH:mm:ss.ffff");
            string query = "INSERT INTO Person (Name,Organisation,TimeIn) VALUES('" + name + "','" + organisation + "','" +strDateTimeIn+ "')";
            SqlCommand SignIn = new SqlCommand(query,Connection);
            SignIn.ExecuteNonQuery();
        }

    }

任何帮助将不胜感激,谢谢

使用的连接字符串是:string connectionString = (@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Register.mdf;Integrated Security=True");

【问题讨论】:

  • @Corak 可能是因为它是 LocalDB(例如like this
  • @DavidG - 啊,谢谢!

标签: c# sql


【解决方案1】:

你需要打开连接

using (Connection = new SqlConnection(connectionString))
{
    connection.Open();

    using (SqlDataAdapter adapter = new SqlDataAdapter("INPUT INTO Person", Connection))//, Connection
        {
            DataTable RegisterTable = new DataTable();
            adapter.Fill(RegisterTable); //System.InvalidOperationException: 'The ConnectionString property has not been initialized.' TO FIX

            string name = textBox1.Text;
            string organisation = textBox3.Text;
            DateTime Time = DateTime.Parse(textBox2.Text);
            string strDateTimeIn = Time.ToString("yyyy-MM-dd HH:mm:ss.ffff");
            string query = "INSERT INTO Person (Name,Organisation,TimeIn) VALUES('" + name + "','" + organisation + "','" +strDateTimeIn+ "')";
            SqlCommand SignIn = new SqlCommand(query,Connection);
            SignIn.ExecuteNonQuery();
        }
}

【讨论】:

    【解决方案2】:

    “ConnectionString 属性尚未初始化” 清楚地表明用于打开SqlConnection 的连接字符串属性在方法内部未正确分配。要解决此问题,请在方法主体中分配连接字符串,如下所示:

    private void AcceptData()
    {
        // assumed the connection string obtained from app.config or web.config file
        string connectionString = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
        using (SqlConnection Connection = new SqlConnection(connectionString))
        {
            Connection.Open(); // open the connection before using adapter
            using (SqlDataAdapter adapter = new SqlDataAdapter("INPUT INTO Person", Connection))
            {
                // other stuff
            }
        }
    
        // other stuff
    }
    

    或者将连接字符串作为方法参数传递:

    private void AcceptData(string connectionString)
    {
        using (SqlConnection Connection = new SqlConnection(connectionString))
        {
            Connection.Open(); // open the connection before using adapter
            using (SqlDataAdapter adapter = new SqlDataAdapter("INPUT INTO Person", Connection))
            {
                // other stuff
            }
        }
    
        // other stuff
    }
    

    参考资料:

    How to fix "The ConnectionString property has not been initialized"

    C# Database The ConnectionString property has not been initialized

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-23
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多