我目前收到錯誤: 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");
@Corak可能因爲它是Loc alDB(例如[像這樣](https://www.connectionstrings.com/sqlconnection/localdb-automatic-instance-with-specific-data-file/)) – DavidG
@DavidG - 啊,謝謝! – Corak