我是计算机科学的学生,并且正在在家中从事一些C#应用程序的练习。我创建了一个本地数据库(.mdf),并尝试通过文本框添加一些数据。
//我有这段代码用于将数据发送到已经连接的数据库。
private void BtnSubmitD_Click(object sender, EventArgs e)
{
cmd.CommandText = "inster into st(Student's ID) values ('"+txtID.Text+"' )";
con.Open();
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Data has beed record.");
}
此错误消息不断出现:
system.data.sqlclient.sqlexception'关键字'into'附近的语法不正确。字符串')'后的右引号引起来。'
答案 0 :(得分:1)
您可以通过以下方式更正您的查询
inster into st(Student's ID) values ('"+txtID.Text+"' );
收件人
insert into st (`Student's ID`) values ('"+txtID.Text+"' );
如果您的列中包含spaces
和'
,请使用``符号将其括起来。
此外,您的查询易于进行Mysql注入。请考虑到这一点。