ExecuteNonQuery 返回值的幾種情況

2022-03-08 03:58:09 字數 962 閱讀 3069

關於executenonquery() 方法

以前對這個一直都沒在意,基本上都沒有用其返回值,查了一下msdn,如下:

sqlcommand.executenonquery 方法對連線執行 transact-sql 語句並返回受影響的行數。備註:可以使用executenonquery 來執行目錄操作(例如查詢資料庫的結構或建立諸如表等的資料庫物件),或通過執行

update、insert 或 delete 語句,在不使用 dataset 的情況下更改資料庫中的資料。雖然executenonquery 不返回任何行,但對映到引數的任何輸出引數或返回值都會用資料進行填充。對於update、insert 和 delete 語句,返回值為該命令所影響的行數。對於所有其他型別的語句,返回值為

-1。如果發生回滾,返回值也為 -1

執行select子句,資料庫並無變化,自然返回-1。同樣適合odbc、ole

db等資料來源,因為都實現idbcommand.executenonquery方法。函式申明:

[visual basic]

public overridable function executenonquery() as integerimplements_idbcommand.executenonquery實現:

idbcommand.executenonquery

對於idbcommand.executenonquery

方法,針對 .net framework 資料提供程式的 connection 物件執行 sql

語句,並返回受影響的行數。可以使用executenonquery 來執行目錄操作(例如查詢資料庫的結構或建立諸如表等的資料庫物件),或通過執行

update、insert 或 delete 語句,在不使用 dataset 的情況下更改資料庫中的資料。雖然executenonquery 不返回任何行,但對映到引數的任何輸出引數或返回值都會用資料進行填充。對於其他所有型別的語句,返回值為 -1

ExecuteNonQuery方法教訓

錯誤原因 我在使用executenonquery 方法的時候,用了select,查了資料才發現犯了如此錯誤!基礎問題!publicintexecutenonquery 返回值 受影響的行數。update insert delete 實現 idbcommand.executenonquery 備註 您...

使用ExecuteNonQuery()運算元據

函式executenonquery 可以對資料庫執行更新,插入,刪除操作,並返回操作影響的行數,若執行語句不是sql語句,則返回 1,應用例項如下 配置檔案資訊 程式 如下 using system using system.data using system.configuration using...

ExecuteNonQuery 的注意點

最近忙個專案,在查詢某個表中是否有資料的時候,我用了executenonquery 並通過判斷值是否大於0來判斷資料的存在與否。結果與我所設想的很不一致,除錯時才發現,其執行後返回的結果是 1,對此我很是不理解,回頭查了下資料,如下顯示 sqlcommand.executenonquery 方法對連...