首页 > 解决方案 > 在 Visual Studio 中插入查询时出错

问题描述

我是这个领域的新手。尝试将文本框中的值插入到我的数据库表中,但出现错误

 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();

在此处输入图像描述

标签: c#sqlsql-servervisual-studio

解决方案


由于您的表被调用table并且这是一个 SQL 保留字,因此您有两种选择:

  1. 更改您的表名。这是您应该考虑的唯一选择,但为了完整性;
  2. 引用表名:

    insert into [table] values....
    

推荐阅读