0

我正在尝试使用 Web 服务将数据插入到使用 Visual Studio 的表中。当我 eun 时,选择方法并输入参数,然后单击 Invoke 按钮,我收到以下错误。我检查了插入语句的语法,并尝试使用不同的语法。但我遇到了同样的错误。我究竟做错了什么?

System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'User'.
  at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
  at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
  at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
  at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
  at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
  at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
  at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
  at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
  at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
  at Service.register(String fname, String lname, String email, String num, Int32 locID)

//向表中插入值的Web方法//

[WebMethod]
public void register(string fname, string lname, string email, string num, int locID)
{
    SqlConnection conn;
    conn = ConnectionManager.GetConnection();
    conn.Open();
    string cmdString = "INSERT into User values(@fname,@lname,@email,@num,@locID)";
    SqlCommand sqlCommand = new SqlCommand(cmdString, conn);
    sqlCommand.CommandType = CommandType.Text;

    sqlCommand.Parameters.Add("@fname", SqlDbType.Text).Value = fname;
    sqlCommand.Parameters.Add("@lname", SqlDbType.Text).Value = lname;
    sqlCommand.Parameters.Add("@email", SqlDbType.Text).Value = email;
    sqlCommand.Parameters.Add("@num", SqlDbType.Text).Value = num;
    sqlCommand.Parameters.Add("@locID", SqlDbType.Text).Value = locID;
    sqlCommand.ExecuteNonQuery();
    conn.Close();
}

///连接管理器类///

public class ConnectionManager
 {
    public static SqlConnection NewCon;
     public static string ConStr ="Data Source=ACER-PC\\SQLEXPRESS;Initial Catalog=DisasterAlert;Integrated Security=True";

    public static SqlConnection GetConnection()
   {
        NewCon = new SqlConnection(ConStr);
        return NewCon;
   }

 }
4

1 回答 1

1

“用户”是 SQL 的保留字。试试这个

"INSERT into [User] values(@fname,@lname,@email,@num,@locID)"
于 2012-06-30T08:30:08.133 回答