2013-05-04 21 views
0
strSQL = @"UPDATE UserLogin 
         SET UserPassword= @paramUserPassword 
         WHERE UserId= @paramUserId"; 
      objOleDbCommand = new OleDbCommand(strSQL, connectionstring); 

      objOleDbCommand.Parameters.AddWithValue("@paramUserId", "1"); 
      objOleDbCommand.Parameters.AddWithValue("@paramUserPassword", "ooo"); 

      objOleDbComm.ExecuteNonQuery(objOleDbCommand); 

其中UserPassword和UserId具有文本數據類型。 表不與上述查詢更新。具有文本數據類型的訪問db中的更新查詢

+0

嘗試在單引號中包裝您的@paramUserPassword – 2013-05-04 05:22:54

+0

無法正常工作.......... – John 2013-05-04 05:26:16

回答

1

您正在錯誤地設置您的OleDbCommand查詢參數。正如在MSDN文章OleDbCommand.Parameters Property中所述,OleDbCommand對象不支持命名參數的使用方式。您將使用問號字符作爲參數的佔位符,然後以與查詢中顯示的順序相同的順序聲明參數。

試試這個:

var strSQL = @"UPDATE UserLogin 
       SET UserPassword= ? 
       WHERE UserId= ?"; 

using (var myConnection = new OleDbConnection(connectionstring)) 
using (var objOleDbCommand = new OleDbCommand(strSQL, myConnection)) { 

    // Parameter used for the SET statement declared before the parameter for the WHERE 
    // clause since this parameter is used before that one in the SQL statement. 
    objOleDbCommand.Parameters.AddWithValue("@paramUserPassword", "ooo"); 
    objOleDbCommand.Parameters.AddWithValue("@paramUserId", "1"); 

    myConnection.Open(); 
    objOleDbCommand.ExecuteNonQuery(); 
} 

此代碼還演示了using statement,這將確保處置該OleDbConnectionOleDbCommand對象所使用的資源時,會退出塊。