Asp.net中SqlDataAdapter和SqlCommand對比分析

來源:互聯網
上載者:User
一、SqlDataAdapter和DateSet原理:DateSet是資料的記憶體駐留表示形式,它提供了獨立於資料來源的一致關係編程模型;從某種程度上說DateSet就是一個不可視的資料庫。但真正與資料來源打交道的是SqlDataAdapter,包括從資料來源填充資料集和從資料集更新資料來源。SqlDataAdapter使用Fill()方法將檢索的資料填充到DateSet。流程:IE——GridView——DataSet——SqlDataAdapter——SQL資料庫public
void BindGrid()   {       SqlDataAdapter myAdp =
new
SqlDataAdapter("select * from UserAdmin order byUserId desc", conn);       DataSet ds =newDataSet();       myAdp.Fill(ds,
"Authors");       //Label1.Text = ds.Tables[0].Rows.Count.ToString();//得到共有多少條記錄;       GridView1.DataSource = ds.Tables["Authors"].DefaultView;       GridView1.DataBind();}二、SqlCommand和SqlDataReade原理:SqlCommand通過ExecuteReader()方法將得到的資料給SqlDataReade對象。SqlDataReade逐行將從資料來源獲得的資料放進緩衝區進行處理。優點:SqlDataReade執行速度快,提高應用程式效能。優點原因:一旦資料可用,SqlDataReade就會立即檢索該資料,而不是等待返回查詢的全部結果;預設情況下,一次只在緩衝區儲存一行,從而降低系統開銷。缺點:使用不靈活。流程:IE——SqlDataReade——SqlCommand——SQL資料庫註:每次使用完SqlDataReade對象後一定要調用Close()方法將其關閉。因為SqlDataReade對象以獨佔的方式使用Connection。例:       SqlCommandmyconn =
newSqlCommand("select * from v_economy2_comiddwhere eid="
+ Request.QueryString["eid"] +
"",conn);        conn.Open();       SqlDataReader rd =myconn.ExecuteReader();       rd.Read();       Lbyear1.Text = rd["year1"].ToString();       Lbmonth1.Text = rd["month1"].ToString();       Lbcom_name.Text = rd["com_name"].ToString();       rd.Close();

       conn.Close();

區別在於:

SqlDataAdapter:
用於填充 DataSet 和更新 SQL 資料庫的“一組”資料命令和“一個”資料庫連接。

SqlDataAdapter不但可以操作多個SQL命令,而且還可以操作一個SQL命令

DataSet是關係型資料庫的抽象,ADO.NET使用DataAdapter(意為資料配接器)作為dataset和資料來源之間的橋樑;DataAdapter提供了Fill()方法從資料庫中擷取資料並產生DataSet。

SqlCommand:
對 SQL 資料庫執行的“一個”SQL 陳述式或預存程序。
SqlCommand只能操作一個SQL命令

個人覺得SqlCommand用來更新少量資料時比較合適,速度快,使用方便。他是執行sql語句查詢的。
SqlDataAdapter用於把資料放到DataSet中,集中修改、刪除後,通過Update把整個DataSet再提交回給資料庫進行處理。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.