C# 在visual studio中插入查询时出错
我是这个领域的新手。试图将textbox中的值插入到我的数据库表中,但在处出错C# 在visual studio中插入查询时出错,c#,sql,sql-server,visual-studio,C#,Sql,Sql Server,Visual Studio,我是这个领域的新手。试图将textbox中的值插入到我的数据库表中,但在处出错 adapter.InsertCommand.ExecuteNonQuery(); 谁能帮我解决这个问题 SqlCommand command; SqlDataAdapter adapter = new SqlDataAdapter(); String sql = "insert into NewName values('" + first_Name.Text + "','" + last_Name.Text
adapter.InsertCommand.ExecuteNonQuery();
谁能帮我解决这个问题
SqlCommand command;
SqlDataAdapter adapter = new SqlDataAdapter();
String sql = "insert into NewName values('" + first_Name.Text + "','" + last_Name.Text + "','" + user.Text + "','" + email.Text + "','" + password.Text + "','" + contact.Text + "')";
command = new SqlCommand(sql,con);
adapter.InsertCommand = new SqlCommand(sql,con);
// this line here is showing the error
adapter.InsertCommand.ExecuteNonQuery();
command.Dispose();
con.Close();
由于您的表名为
table
,并且是SQL保留字,因此您有两种选择:
insert into [table] values....
由于您的表名为
table
,并且是SQL保留字,因此您有两种选择:
insert into [table] values....
您没有在insert上列出列名。这意味着您也在尝试插入标识列。始终列出您的列名
insert into NewName (firstname, lastname, username, email, password, contact)
values('" + first_Name.Text + "','" + last_Name.Text + "','" + user.Text + "','" + email.Text + "','" + password.Text + "','" + contact.Text + "')
您没有在insert上列出列名。这意味着您也在尝试插入标识列。始终列出您的列名
insert into NewName (firstname, lastname, username, email, password, contact)
values('" + first_Name.Text + "','" + last_Name.Text + "','" + user.Text + "','" + email.Text + "','" + password.Text + "','" + contact.Text + "')
是的,我已经这样做了。我在表列中使用了“user”,这是不允许的。更改列名后,一切正常。 这是密码
SqlCommand command;
SqlDataAdapter adapter = new SqlDataAdapter();
String sql = "insert into NewName values('" + first_Name.Text + "','" + last_Name.Text + "','" + user.Text + "','" + email.Text + "','" + password.Text + "','" + contact.Text + "')";
command = new SqlCommand(sql, con);
adapter.InsertCommand = new SqlCommand(sql, con);
// this line here is showing the error
adapter.InsertCommand.ExecuteNonQuery();
command.Dispose();
con.Close();
是的,我已经这样做了。我在表列中使用了“user”,这是不允许的。更改列名后,一切正常。 这是密码
SqlCommand command;
SqlDataAdapter adapter = new SqlDataAdapter();
String sql = "insert into NewName values('" + first_Name.Text + "','" + last_Name.Text + "','" + user.Text + "','" + email.Text + "','" + password.Text + "','" + contact.Text + "')";
command = new SqlCommand(sql, con);
adapter.InsertCommand = new SqlCommand(sql, con);
// this line here is showing the error
adapter.InsertCommand.ExecuteNonQuery();
command.Dispose();
con.Close();
你真的有一个名为“table”的表吗?
table
是SQL保留字,你真的有一个名为table的表吗?@DavidG你和我说的是同一种语言……我建议你为你的表取一个更好的名称,而不是table
。我还强烈建议将SQL参数化。SQL注入不是你的朋友。@apomene,SQL server在使用括号时出人意料地宽容,如果你喜欢,甚至可以使用不可打印的字符(看着世界燃烧)。你真的有一个名为“table”的表吗?table
是SQL保留字,你真的有一个名为table的表吗?@DavidG you和我,我们说的是同一种语言……我建议您为您的表选择一个更好的名称,而不是表
。我还强烈建议将SQL参数化。SQL注入不是你的朋友。@apomene,SQL server在使用括号时出人意料地宽容,如果你愿意,甚至可以使用不可打印的字符(看着世界燃烧)。我要补充的是,OP必须在bobby tables访问之前开始在查询中使用参数@NavidAnjum你也会犯同样的错误吗?提示:答案是否定的,因为您还更改了错误消息。我已经回答了您的问题,您现在看到了一个新问题。我要补充的是,OP必须在bobby tables访问之前开始在查询中使用参数@NavidAnjum你也会犯同样的错误吗?提示:答案是否定的,因为您还更改了错误消息。我已经回答了您的问题,您现在看到了一个新问题。添加列名后,它再次显示错误。关键字“user”附近的语法不正确。@NavidAnjum在执行之前输出您的sql
,并查看是否有语法错误。添加列名后,它再次显示错误。关键字“user”附近的语法不正确。@NavidAnjum在执行之前输出您的sql
,并查看是否有语法错误。