小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

SqlCommand和SqlDataAdapter的區(qū)別

 小茂。 2012-02-14

SqlDataAdapter對象

一、特點介紹
1、表示用于填充 DataSet 和更新 SQL Server 數(shù)據(jù)庫的一組數(shù)據(jù)命令和一個數(shù)據(jù)庫連接。
2、在SqlDataAdapter和DataSet之間沒有直接連接。當完成SqlDataAdpater.Fill(DataSet)調(diào)用后,兩個對象之間就沒有連接了。
二、使用介紹
1、創(chuàng)建SqlDataAdapter
    string strSQL=“Select * from Customers”;
    SqlCommand cmd=new SqlCommand(strSQL,cn);
    SqlDataAdapter da=new SqlDataAdapter();
    da.SelectCommand=cmd;
2、SqlDataAdapter構造函數(shù)
①stringstrConn=“Provider=.....”;
  string strSQL=“select * from Customers”;
  SqlDataAdapter da=new SqlDataAdapter(strSQL,strConn);
②stringstrConn=“Provider=.....”;
  SqlConnection cn=new SqlConnection(strConn);
  SqlDataAdapter da=new SqlDataAdapter(“select * from Customers”,cn);
③stringstrConn=“Provider=.....”;
  string strSQL=“select * from Customers”;
  SqlConnection cn=new SqlConnection(strConn);
  SqlCommand cmd=new SqlCommand(strSQL,cn);
  SqlDataAdapter da=new SqlDataAdapter(cmd);
3、從查詢中獲取結果
①使用Fill方法
    SqlDataAdapter da=new SqlDataAdapter(strSQL,strConn);
    DataSet ds=new DataSet();
    da.Fill(ds);   //這里ds中的表名為Table
②使用Fill方法創(chuàng)建DataTable對象和DataColumn對象
    SqlDataAdapter da=new SqlDataAdapter(strSQL,strConn);
    da.TableMapping.Add(“Table”,“Customers”);    
    DataSet ds=new DataSet();
    da.Fill(ds);
③使用重載Fill方法
    SqlDataAdapter.Fill(DataSet,“Customers”);
    SqlDataAdapter.Fill(DataTable);
   SqlDataAdapter.Fill(DataSet,intStartRecord,intNumRecords,“TableName”);
④開放和關閉連接
    如果調(diào)用了一個SqlDataAdapter對象的Fill方法,而SelectCommand屬性的Connection關閉了,那么SqlDataAdapter就會開放一個連接,然后提交查詢、獲取結果、最后關閉連接。如果在調(diào)用前開放了Connection,那么操作之后仍然保持開放。
    SqlDataAdapter daCustomers,daOrders;
    daCustomers=new SqlDataAdapter(“Select * fromCustomers”,cn);
    daOrders=new SqlDataAdapter(“Select * from Orders”,cn);
    DataSet ds=new DataSet();
    cn.Open();
    daCustomers.Fill(ds);
    daOrders.Fill(ds);
    cn.Close();
⑤多次調(diào)用Fill方法
    刷新DataSet中的數(shù)據(jù),最簡單的解決方法就是清空DataSet(或DataTable),然后再次調(diào)用DataAdapter對象的Fill方法。    
三、屬性方法事件介紹 
1、屬性
①AcceptChangeDuringFill:確定由DataAdapter所獲取的行的RowState(默認為True)。
②DeleteCommand:獲取或設置一個 Transact-SQL 語句或存儲過程,以從數(shù)據(jù)集刪除記錄。
③InsertCommand:獲取或設置一個 Transact-SQL 語句或存儲過程,以在數(shù)據(jù)源中插入新記錄。
④SelectCommand:獲取或設置一個 Transact-SQL 語句或存儲過程,用于在數(shù)據(jù)源中選擇記錄。
⑤UpdateCommand:獲取或設置一個 Transact-SQL 語句或存儲過程,用于更新數(shù)據(jù)源中的記錄。
⑥TableMappings:SqlDataAdapter用來將查詢的結果映射到DataSet的信息集合。
⑦ContinueUpdate:控制SqlDataAdapter在遇到一個錯誤之后是否繼續(xù)提交更改(默認為false)。
2、方法
①Fill:執(zhí)行存儲于SelectCommand中的查詢,并將結果存儲在DataTable中。
②FillSchema:為存儲在SelectCommand中存儲的查詢獲取架構信息。獲取查詢中的各列名稱和數(shù)據(jù)類型。
③GetFillParameters:為SelectCommand獲取一個包含著參數(shù)的數(shù)組。
④Update:向數(shù)據(jù)庫提交存儲在DataSet(或DataTable、DataRows)中的更改。該方法會返回一個整數(shù)值,其中包含著在數(shù)據(jù)存儲中成功更新的行數(shù)。
3、事件
①FillError:當DataAdapter遇到填充DataSet或DataTable的一個錯誤時,該事件被觸發(fā)。
②RowUpdated:向數(shù)據(jù)庫提交一個修改的行之后被觸發(fā)。
③RowUpdating:向數(shù)據(jù)庫提交一個修改的行之前被觸發(fā)。

SqlCommand對象

介紹
SqlCommand對象允許你指定在數(shù)據(jù)庫上執(zhí)行的操作的類型。比如,你能夠對數(shù)據(jù)庫中的行數(shù)據(jù)執(zhí)行select,insert,modify以及delete命令。SqlCommand對象能被用來支持斷開連接數(shù)據(jù)管理的情況,但是在這節(jié)課我們將只單獨使用SqlCommand對象。后面關于SqlDataAdapter的課程將解釋如何使用斷開數(shù)據(jù)實現(xiàn)應用程序。這節(jié)課將同時展示如何從數(shù)據(jù)庫中返回一個單獨的值,比如表中記錄的數(shù)量。
創(chuàng)建SqlCommand對象
SqlCommand cmd = new SqlCommand("select CategoryName fromCategories", conn);
上面一行是典型的實例化SqlCommand對象的代碼。它使用一個string參數(shù)來保存你想要執(zhí)行的命令以及一個關于SqlConnection對象的引用。SqlCommand具有重載形式,這些形式你將在以后的示例中看到。
查詢數(shù)據(jù)
當使用SQL的select命令,會得到一組數(shù)據(jù)集。為了和SqlCommand對象配合使用,你應該使用ExecuteReader方法,它返回一個SqlDataReader對象。我們將在后面的內(nèi)容討論SqlDataReader。下面的例子顯示了如何使用SqlCommand對象來得到SqlDataReader對象:
// 1. Instantiate a new command with a query and connection
SqlCommand cmd = new SqlCommand("select CategoryName fromCategories", conn);
// 2. Call Execute reader to get query results
SqlDataReader rdr = cmd.ExecuteReader();
在上面的示例中,我們通過傳遞命令字符串核連接對象到構造函數(shù)的方式實體化了SqlCommand對象。然后我們通過SqlCommand對象cmd調(diào)用ExecuteReader方法得到了SqlDataReader對象。
這些代碼是表1中ReadData方法的一部分,我們將在后面集中介紹。
插入數(shù)據(jù)
要對數(shù)據(jù)庫插入數(shù)據(jù),使用SqlCommand對象的ExecuteNonQuery方法。下面的代碼顯示了如何向數(shù)據(jù)庫表插入數(shù)據(jù): 
string insertString = @"insert into Categories(CategoryName,Description)values ('Miscellaneous', 'Whatever doesn''t fit elsewhere')";
SqlCommand cmd = new SqlCommand(insertString, conn);
cmd.ExecuteNonQuery();
SqlCommand的實例化過程與以前看到的有一些區(qū)別,但是基本一致。在構造函數(shù)的第一個字符串參數(shù)中是用的是插入字符串變量而不三字符串字面值。該變量在SqlCommand聲明之前被聲明了。
注意在insertString文本中“doesn’’t”的兩個單引號(’’)。這是將它轉義為適當?shù)膯我枴?BR>另外一個需要注意的是我們顯式指明了列:CategoryName和Description。列表中有一個主鍵名為CategoryID。我們忽略這列因為SQL Server將自動添加此字段。試圖對主鍵比如CategoryID添加值會產(chǎn)生異常。
為了執(zhí)行此命令,我們簡單的對SqlCommand實體cmd調(diào)用ExecuteNonQuery方法。
這段代碼是表1中InsertData方法的一部分,我們將在后面集中介紹。
更新數(shù)據(jù)
ExecuteNonQuery方法同樣用來更新數(shù)據(jù)。下面的代碼顯示了如何更新數(shù)據(jù):
string updateString = @"update Categories set CategoryName = 'Other'
  where CategoryName = 'Miscellaneous'";
SqlCommand cmd = new SqlCommand(updateString);
cmd.Connection = conn;
cmd.ExecuteNonQuery();
刪除數(shù)據(jù)
你同樣能夠使用ExecuteNonQuery方法刪除數(shù)據(jù)。下面的例子說明了如何使用EXecuteNonQuery方法刪除數(shù)據(jù)庫中的記錄。 
string deleteString = @"delete from Categories where CategoryName ='Other'";
SqlCommand cmd = new SqlCommand();
cmd.CommandText = deleteString;
cmd.Connection = conn;
cmd.ExecuteNonQuery();
這個示例使用了沒有參數(shù)的SqlCommand構造函數(shù)。取而代之的是顯式地設置了CommandText和SqlCommand對象的連接屬性。
我們同樣能夠使用SqlCommand構造函數(shù)在前面的兩個重載形式——用來插入或者更新命令——得到相同的結果。它說明了在任何時候既能夠改變命令文本又能夠改變連接對象。
ExecuteNonQuery方法調(diào)用將命令傳遞給數(shù)據(jù)庫。
得到單一值
某些時候你想從數(shù)據(jù)庫中只取一個值,它可能是關于數(shù)據(jù)集的計數(shù)、和、平均值或者其他聚合數(shù)值。使用ExecuteReader方法并計算代碼中的結果并不是做這些事情的有效方式。最好的選擇就是讓數(shù)據(jù)庫能夠執(zhí)行并且只返回你所需要的單獨的值。下面的示例說明了如何使用ExecuteScalar方法來實現(xiàn):
SqlCommand cmd = new SqlCommand("select count(*) from Categories",conn);
  int count = (int)cmd.ExecuteScalar(); 
總結
SqlCommand對象允許你擦許并對數(shù)據(jù)庫傳送命令。它含有針對不同的命令而特定的方法。ExecuteReader方法返回SqlDataReader對象來現(xiàn)實查詢的結果。對于insert,update以及delete這些SQL命令,使用ExecuteNonQuery方法。如果你只需要查詢的單獨聚集值,ExecuteScalar方法是最好的選擇

SqlDataAdapter和SqlCommand對比分析

一、SqlDataAdapter和DateSet

原理:DateSet是數(shù)據(jù)的內(nèi)存駐留表示形式,它提供了獨立于數(shù)據(jù)源的一致關系編程模型;從某種程度上說DateSet就是一個不可視的數(shù)據(jù)庫。但真正與數(shù)據(jù)源打交道的是SqlDataAdapter,包括從數(shù)據(jù)源填充數(shù)據(jù)集和從數(shù)據(jù)集更新數(shù)據(jù)源。SqlDataAdapter使用Fill()方法將檢索的數(shù)據(jù)填充到 DateSet。

流程:IE——GridView——DataSet——SqlDataAdapter——SQL數(shù)據(jù)庫

public void BindGrid()

{

SqlDataAdapter myAdp = new SqlDataAdapter("select * fromUserAdmin order by UserId desc", conn);

DataSet ds = new DataSet();

myAdp.Fill(ds, "Authors");

//Label1.Text = ds.Tables[0].Rows.Count.ToString(); //得到共有多少條記錄;

GridView1.DataSource =ds.Tables["Authors"].DefaultView;

GridView1.DataBind();

}

二、SqlCommand和SqlDataReade

原理:SqlCommand通過ExecuteReader()方法將得到的數(shù)據(jù)給SqlDataReade對象。

SqlDataReade逐行將從數(shù)據(jù)源獲得的數(shù)據(jù)放進緩沖區(qū)進行處理。

優(yōu)點:SqlDataReade執(zhí)行速度快,提高應用程序性能。

優(yōu)點原因:一旦數(shù)據(jù)可用,SqlDataReade就會立即檢索該數(shù)據(jù),而不是等待返回查詢的全部結果;默認情況下,一次只在緩沖區(qū)存儲一行,從而降低系統(tǒng)開銷。

缺點:使用不靈活。

流程:IE——SqlDataReade——SqlCommand——SQL數(shù)據(jù)庫

注:每次使用完SqlDataReade對象后一定要調(diào)用Close()方法將其關閉。因為SqlDataReade對象以獨占的方式使用Connection。

例:

SqlCommand myconn = new SqlCommand("select * fromv_economy2_comidd where 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();

區(qū)別在于:
SqlDataAdapter:
用于填充 DataSet 和更新 SQL 數(shù)據(jù)庫的“一組”數(shù)據(jù)命令和“一個”數(shù)據(jù)庫連接。
SqlDataAdapter不但可以操作多個SQL命令,而且還可以操作一個SQL命令

DataSet是關系型數(shù)據(jù)庫的抽象,ADO.NET使用DataAdapter(意為數(shù)據(jù)適配器)作為dataset和數(shù)據(jù)源之間的橋梁;DataAdapter 提供了Fill()方法從數(shù)據(jù)庫中獲取數(shù)據(jù)并生成DataSet。
SqlCommand:
對 SQL 數(shù)據(jù)庫執(zhí)行的“一個”SQL語句或存儲過程。
SqlCommand只能操作一個SQL命令

個人覺得SqlCommand用來更新少量數(shù)據(jù)時比較合適,速度快,使用方便。他是執(zhí)行sql語句查詢的。
SqlDataAdapter用于把數(shù)據(jù)放到DataSet中,集中修改、刪除后,通過Update把整個DataSet再提交回給數(shù)據(jù)庫進行處理

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多