2017-09-26 231 views
0

我目前收到错误: System.InvalidOperationException:'ConnectionString属性尚未初始化。' 我想一些帮助来解决这个问题,我想从C#的形式输入数据(多个文本框)到一个SQL数据库构成了我当前的代码是'ConnectionString属性尚未初始化。' TO FIX

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");

+0

@Corak可能因为它是Loc alDB(例如[像这样](https://www.connectionstrings.com/sqlconnection/localdb-automatic-instance-with-specific-data-file/)) – DavidG

+0

@DavidG - 啊,谢谢! – Corak

回答

2

你需要打开连接

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

“ 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

相关问题