要修复'The ConnectionString property has not been initialized.'



我当前正在遇到错误:System.InvalidoperationException:"连接属性尚未初始化"。当我试图将数据从C#表单(多个文本框)输入到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");

您需要打开连接

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();
        }
}

"连接属性尚未初始化" 清楚地表明,打开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
}

参考:

如何修复" ConnectionString属性"尚未初始化

c#数据库连接属性尚未初始化

相关内容

最新更新