關於C#操作數據庫ExecuteNonQuery()的返回值問題


 1 if (AccessCon.ExecuteSql(sql = "select * from salesData where index ='" + man.Index + "'") > 0)
 2             {
 3                 retValue = AccessCon.ExecuteSql(sql = "update salesData set sellingPrize='" + man.SellingPrize + "' where index='" + man.Index + "'");
 4                 Console.WriteLine(sql);
 5             }
 6             else
 7             {
 8                 retValue = AccessCon.ExecuteSql(sql = "insert into salesData values ('" + man.Index + "','" + man.SellingPrize + "')");
 9                 Console.WriteLine(sql);
10             }
11 
12             return retValue;

  本來想通過select * from salesData where index =' man.Index '執行后的返回值是否大於0判斷表中是否已有數據

因為msdn中說返回受影響的行數:

Executes a Transact-SQL statement against the connection and returns the number of rows affected.

但是卻沒看到備注里說

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

對於UPDATE, INSERT,和 DELETE返回受影響的函數,但是對於所有其他類型的語句,返回值為 -1。如果發生回滾,返回值也為 -1。這樣對於search語句來說就不適合ExecuteNonQuery()方法了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM