我已将由 sql server 创建的本地数据库连接到我在 visual studio (C#) 中的项目。现在我希望将用户在文本字段中给出的数据输入到我的数据库中。这是我尝试做的事情
private void Button_AddCustomer_Click(object sender, EventArgs e)
{
try
{
//SqlConnection objsqlconn = new SqlConnection(conn);
SqlConnection myConnection = new SqlConnection("Data Source=SHIRWANIPC;" +
"Initial Catalog=TEST DATABASE;" + "Integrated Security=True");
myConnection.Open();
SqlCommand objcmd = new SqlCommand("INSERT INTO
Customer(PhoneNumber,MobileNumber,Address) VALUES (a, b, c)", myConnection);
objcmd.ExecuteNonQuery();
}
catch(SqlException ex)
{
MessageBox.Show(ex.ToString());
}
}
它抛出一个异常说明无效的列名a,无效的列名b,无效的列名c
。问题是什么以及如何使用插入查询将用户输入到我的数据库中?我正在使用 visual studio C#,本地数据库是使用 ms sql 创建的。
最佳答案
替换
VALUES (a, b, c)
与
VALUES (' + textBox1.value + (other text area) + ')'
无论如何在查询之前检查输入!
好的
SqlCommand objcmd = new SqlCommand("INSERT INTO Customer(PhoneNumber,MobileNumber,Address) VALUES ('" + PhoneNumber.Text + "', '" + MobileNumber.Text + "', '" + Address.Text + "')", myConnection);
关于c# - 通过事件处理程序将值插入表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22094154/