0

我正在使用以下代码更新用户信息,但出现错误。谁能指出我出了什么问题。我正在为 Web 服务编写此代码。

public string UpdateUser(int uID, string fName,
            string lName, string password, string emailAddress)
        {
            // Create connection object
            int ix = 0;
            string rTurn = "";
            OleDbConnection oleConn = new OleDbConnection(connString);
            try
            {
                oleConn.Open();
                string sql = "UPDATE [User] SET [fName]=@fName, [lName]=@lName, [password]=@password, [emailAddress]=@emailAddress" + "WHERE [ID]=@uID";
                OleDbCommand oleComm = new OleDbCommand(sql, oleConn);

                oleComm.Parameters.Add("@fName", OleDbType.Char).Value = fName;
                oleComm.Parameters.Add("@lName", OleDbType.Char).Value = lName;
                oleComm.Parameters.Add("@password", OleDbType.Char).Value = password;
                oleComm.Parameters.Add("@emailAddress", OleDbType.Char).Value = emailAddress;
                oleComm.Parameters.Add("@uID", OleDbType.Integer).Value = uID;

                ix = oleComm.ExecuteNonQuery();
                if (ix > 0)
                    rTurn = "User Updated";
                else
                    rTurn = "Update Failed";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                rTurn = ex.ToString();
            }
            finally
            {
                oleConn.Close();
            }
            return rTurn;
        }

错误 *当我尝试更新用户时出现以下错误。*

    <string>
System.Data.OleDb.OleDbException: Syntax error (missing operator) in query expression '@emailAddressWHERE [ID]=@uID'.
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
       at System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
       at UserManagement.UserRegistration.UpdateUser(Int32 uID, String fName, String lName, String password, String emailAddress) in C:\Users\smartamrit\Desktop\SystemSoftware\UserManagement\UserRegistration.asmx.cs:line 97
</string>
4

4 回答 4

4

您需要先添加额外的空间WHERE

string sql = @"UPDATE [User] 
              SET [fName]=@fName, 
                  [lName]=@lName, [password]=@password, 
                  [emailAddress]=@emailAddress" + " WHERE [ID]=@uID";
                                                   ^ here

或者如果你没有连接它我看不出有什么区别,为什么不直接做

string sql = @"UPDATE [User] 
               SET [fName]=@fName, 
                   [lName]=@lName, 
                   [password]=@password, 
                   [emailAddress]=@emailAddress 
               WHERE [ID]=@uID";
于 2012-10-18T15:27:42.487 回答
2

该错误是不言自明的:

'@emailAddressWHERE [ID]=@uID'

@emailAddress和之间没有空格WHERE

于 2012-10-18T15:27:32.907 回答
2

WHERE您需要在此处之前有一个空格

@emailAddress" + " WHERE 
于 2012-10-18T15:27:34.060 回答
1

您需要在开始之前添加空间。

您可以像下面提到的那样编写查询。

string sql = "UPDATE [User] SET [fName]=@fName, [lName]=@lName, 
    [password]=@password, [emailAddress]=@emailAddress" +" "+ "WHERE [ID]=@uID";
于 2012-10-18T15:38:20.070 回答