溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

ADO.NET SqlConnection怎么使用

發(fā)布時(shí)間:2021-12-03 15:40:19 來源:億速云 閱讀:148 作者:iii 欄目:編程語言

這篇文章主要講解了“ADO.NET SqlConnection怎么使用”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“ADO.NET SqlConnection怎么使用”吧!

ADO.NET框架支持兩種模式的數(shù)據(jù)訪問:連接模式(Connected)和非連接模式(disconnected)。利用ADO.NET中的 Connection,Command,DataReader來獲取和修改數(shù)據(jù)庫中的數(shù)據(jù) 。使用RetrieveStatistics()方法獲得數(shù)據(jù)命令執(zhí)行時(shí)的統(tǒng)計(jì)信息,例如,可以獲取總命令執(zhí)行時(shí)間的統(tǒng)計(jì)信息。

ADO.NET SqlConnection統(tǒng)計(jì)信息有以下常用屬性可以獲得:
◆BytesReceived : 查詢中接收到的字節(jié)數(shù)
◆BytesSend : 發(fā)送出數(shù)據(jù)的字節(jié)數(shù)
◆ConnectionTime : 當(dāng)前連接被開啟的總時(shí)間
◆ExecutionTime : 返回以毫秒為單位的連接執(zhí)行時(shí)間
◆IduCount: 用于返回被執(zhí)行Insert、Update、Delete命令的次數(shù)
◆IduRows : 用于返回被執(zhí)行Insert、Update、Delete命令的行數(shù)
◆SelectCount: 用于返回Select命令執(zhí)行的次數(shù)
◆SelectRows : 用于返回Select命令執(zhí)行的行數(shù)

ADO.NET SqlConnection案例分析:取得數(shù)據(jù)庫查詢的執(zhí)行時(shí)間 

// Movies類中的GetAll方法返回一個(gè)List對象,該對象可以被GridView等控件做為數(shù)據(jù)源綁定  namespace  DawnDataObject  {  public class Movies  {  public static readonly string _connectionString;    // 連接數(shù)據(jù)庫字符串為靜態(tài)成員,每個(gè)實(shí)例共享。   static Movies(){  _connectionString = WebConfigurationManager.ConnectionStrings["DawnEnterpriseDBConnectionString"].  ConnectionString;  }   private string _title;  private string _director;   // Movies類中包括的屬性有Title、Director  public string Title{  get { return _title; }  set { _title = value; }  }  public string Director {  get { return _director; }  set { _director = value; }  }   // Movies類中的GetAll方法返回一個(gè)List對象,該對象可以被GridView等控件做為數(shù)據(jù)源綁定  public List<Movies> GetAll(out long executeTime)    // executeTime作為out參數(shù)  {  List<Movies> result = new List<Movies>();  SqlConnection conn = new SqlConnection(_connectionString);  SqlCommand comm = new SqlCommand("WAITFOR DELAY '0:0:03';select Title,Director from Movies", conn);  conn.StatisticsEnabled = true;   // 開啟獲取統(tǒng)計(jì)信息的功能  using(conn){  // using關(guān)鍵字指定了conn一旦離開這個(gè)代碼段,自動調(diào)用其Dispose函數(shù)  conn.Open();  SqlDataReader reader = comm.ExecuteReader();  while(reader.Read()){  Movies newnewmovie = new Movies();  newmovie._title = (string)reader["Title"];  newmovie._director = (string)reader["Director"];  result.Add(newmovie);  }  IDictionary stats = conn.RetrieveStatistics();  executeTime = (long)stats["ExecutionTime"];  return result;  }  }  }  }

感謝各位的閱讀,以上就是“ADO.NET SqlConnection怎么使用”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對ADO.NET SqlConnection怎么使用這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI