2017-09-26 272 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

相關問題