【发布时间】: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 - 啊,谢谢!