新聞中心
ADO.NET2.0的特性想必大家也都體驗到它的好處了,它的新特性讓你愛不釋手,所謂一分耕耘,一分收獲,付出了就一定會有回報的,說了這么多還是希望大家多了解了解ADO.NET原理機制,我們這里就先不和大家說關(guān)于原理的分析了,我們就ADO.NET SqlConnection的一些新特性來分析一下吧。

創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比寧津網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式寧津網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋寧津地區(qū)。費用合理售后完善,10多年實體公司更值得信賴。
#T#ADO.NET框架支持兩種模式的數(shù)據(jù)訪問:連接模式(Connected)和非連接模式(disconnected)。這一節(jié)介紹如何使用連接模式訪問數(shù)據(jù)庫中的數(shù)據(jù),利用ADO.NET中的 Connection,Command,DataReader來獲取和修改數(shù)據(jù)庫中的數(shù)據(jù) 。使用RetrieveStatistics()方法獲得數(shù)據(jù)命令執(zhí)行時的統(tǒng)計信息,例如,可以獲取總命令執(zhí)行時間的統(tǒng)計信息。
ADO.NET SqlConnection統(tǒng)計信息有以下常用屬性可以獲得:
◆BytesReceived : 查詢中接收到的字節(jié)數(shù)
◆BytesSend : 發(fā)送出數(shù)據(jù)的字節(jié)數(shù)
◆ConnectionTime : 當(dāng)前連接被開啟的總時間
◆ExecutionTime : 返回以毫秒為單位的連接執(zhí)行時間
◆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í)行時間
- // Movies類中的GetAll方法返回一個List對象,該對象可以被GridView等控件做為數(shù)據(jù)源綁定
- namespace DawnDataObject
- {
- public class Movies
- {
- public static readonly string _connectionString;
- // 連接數(shù)據(jù)庫字符串為靜態(tài)成員,每個實例共享。
- 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方法返回一個List對象,該對象可以被GridView等控件做為數(shù)據(jù)源綁定
- public List
GetAll(out long executeTime) - // executeTime作為out參數(shù)
- {
- List
result = new List (); - 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)計信息的功能
- using(conn){ // using關(guān)鍵字指定了conn一旦離開這個代碼段,自動調(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;
- }
- }
- }
- }
分享標(biāo)題:簡單了解ADO.NETSqlConnection新特性
本文來源:http://m.fisionsoft.com.cn/article/coesggd.html


咨詢
建站咨詢
