executenonquery 的用法詳解
executenonquery 的用法詳解
C#中操作資料庫技術之ExecuteNonQuery用法
最近在補基礎知識,剛好補到C#中對資料庫操作的一些技術,今天學習了ExecuteNonQuery的東西,看自己專案維護專案的程式碼和網路資料查詢,基本上搞懂了ExecuteNonQuery的用法,小小的做個總結,供以後查閱。
ExecuteNonQuery方法主要用來更新數據,當然也可以用來執行目標操作(例如查詢資料庫的結構或建立諸如表等的資料庫物件)。通常用它來執行insert、update、delete語句,在不使用Dataset的情況下更改資料庫中的資料。 select語句不適合ExecuteNonQuery()方法。
推薦《C 影片教學》
一、首先,來看看ExecuteNonQuery的回傳值:
1. 對於Update、 insert、Delete語句執行成功是傳回值為該指令所影響的行數,如果影響的行數是0,則回傳值就是0;
2. 對於所有其他類型的語句,傳回值為-1;
3. 如果發生回滾,回傳值也為-1;
4. 我們通常在更新操作,判斷回傳值是否大於0,這是沒有問題的。但是對於其他的操作【如對資料結構的操作(建表等)】如果操作成功返回值卻是-1,但是要注意一下啊,例如給資料庫添加一個新表,創建成功返回-1,如果操作失敗就會發生異常,所有執行這種操作最好用Try,Catch語句來捕捉異常。
二、command物件透過ExecuteNonQuery方法更新資料庫的過程非常簡單,步驟如下:
1. 建立資料庫連線;
2. 建立Command對象,並指定一個SQL Inser、Update、Delete查詢或預存程序;
## 3. 將Command物件依附到資料庫連線上; 4. 呼叫ExecuteNonQuery()方法; 5.關閉連線。 三、程式碼範例使用方法: 1. 首先是一個很簡單的類,裡面提供瞭如何用command物件透過ExecuteNonQuery方法跟新資料庫。public class ExecuteNonQueryClas { private static string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString; //as this method provided static method, set the constructor to priviate to prevent create instance with 'new ExecuteNonQuery()' private ExecuteNonQueryClas() { } public static int ExecuteNonQuery(string commandText) { return ExecuteNonQuery(commandText, (SqlParameter[])null); } public static int ExecuteNonQuery(string commandText,SqlParameter[] commandParams) { //if connectionString is null, then throw exception if(connectionString == null || connectionString.Length == 0) throw new ArgumentNullException("connectionString"); using(SqlConnection conn = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(commandText,conn); if (conn.State != ConnectionState.Open) conn.Open(); //check if the commandParams is not null, then attach params to command if(commandParams !=null) AttachParameters(cmd,commandParams); int recordsAffected = cmd.ExecuteNonQuery(); return recordsAffected; } } private static void AttachParameters(SqlCommand cmd,SqlParameter[] commandParams) { if (cmd == null) throw new ArgumentException("command"); if (commandParams != null) { foreach (SqlParameter p in commandParams) { if (p != null) { //// Check for derived output value with no value assigned if ((p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Input) && (p.Value == null)) { p.Value = DBNull.Value; } cmd.Parameters.Add(p); } } } } }
static void Main(string[] args) { string userName = Console.ReadLine(); string loginId = "user"; string sqlString = "update Users set UserName = @name where LoginID= @loginID"; SqlParameter[] parms ={ new SqlParameter("@name",userName), new SqlParameter("@loginID",loginId) }; int rlt = ExecuteNonQueryClas.ExecuteNonQuery(sqlString,parms); Console.WriteLine(rlt); Console.Read(); }
以上是executenonquery 的用法詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

C#.NET應用的測試策略包括單元測試、集成測試和端到端測試。 1.單元測試確保代碼的最小單元獨立工作,使用MSTest、NUnit或xUnit框架。 2.集成測試驗證多個單元組合的功能,常用模擬數據和外部服務。 3.端到端測試模擬用戶完整操作流程,通常使用Selenium進行自動化測試。

C#.NET面試問題和答案包括基礎知識、核心概念和高級用法。 1)基礎知識:C#是微軟開發的面向對象語言,主要用於.NET框架。 2)核心概念:委託和事件允許動態綁定方法,LINQ提供強大查詢功能。 3)高級用法:異步編程提高響應性,表達式樹用於動態代碼構建。

C#是一種現代、面向對象的編程語言,由微軟開發並作為.NET框架的一部分。 1.C#支持面向對象編程(OOP),包括封裝、繼承和多態。 2.C#中的異步編程通過async和await關鍵字實現,提高應用的響應性。 3.使用LINQ可以簡潔地處理數據集合。 4.常見錯誤包括空引用異常和索引超出範圍異常,調試技巧包括使用調試器和異常處理。 5.性能優化包括使用StringBuilder和避免不必要的裝箱和拆箱。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.

C#.NET依然重要,因為它提供了強大的工具和庫,支持多種應用開發。 1)C#結合.NET框架,使開發高效便捷。 2)C#的類型安全和垃圾回收機制增強了其優勢。 3).NET提供跨平台運行環境和豐富的API,提升了開發靈活性。

C#高級開發者面試需要掌握異步編程、LINQ、.NET框架內部工作原理等核心知識。 1.異步編程通過async和await簡化操作,提升應用響應性。 2.LINQ以SQL風格操作數據,需注意性能。 3..NET框架的CLR管理內存,垃圾回收需謹慎使用。

c#.netissutableforenterprise-levelapplications withemofrosoftecosystemdueToItsStrongTyping,richlibraries,androbustperraries,androbustperformance.however,itmaynotbeidealfoross-platement forment forment forment forvepentment offependment dovelopment toveloperment toveloperment whenrawspeedsportor whenrawspeedseedpolitical politionalitable,

C#和.NET的安全最佳實踐包括輸入驗證、輸出編碼、異常處理、以及身份驗證和授權。 1)使用正則表達式或內置方法驗證輸入,防止惡意數據進入系統。 2)輸出編碼防止XSS攻擊,使用HttpUtility.HtmlEncode方法。 3)異常處理避免信息洩露,記錄錯誤但不返回詳細信息給用戶。 4)使用ASP.NETIdentity和Claims-based授權保護應用免受未授權訪問。
