Sql更新表

时间:2012-10-18 15:26:34

标签: asp.net sql visual-studio-2010

我使用以下代码更新用户信息,但我收到错误。任何人都可以指出我的错误。我正在为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 个答案:

答案 0 :(得分: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";

答案 1 :(得分:2)

错误非常明显:

'@emailAddressWHERE [ID]=@uID'

@emailAddressWHERE

之间没有空格

答案 2 :(得分:2)

在此WHERE

之前需要一个空格
@emailAddress" + " WHERE 

答案 3 :(得分:1)

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

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

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