亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

C# 之 DataReader 和 DataSet 的區(qū)別

オリジナル 2016-11-02 16:31:10 700
サマリー:1. 獲取數(shù)據(jù)的方式[1]DataReader 為在線操作數(shù)據(jù), DataReader會一直占用SqlConnection連接,在其獲得數(shù)據(jù)過程中其它操作不可以再使用SqlConnection連接對象。代碼如下:while(datareader.read()) { .............. } dataview.datasource=datareader; dataview.databind(

1. 獲取數(shù)據(jù)的方式
[1]DataReader 為在線操作數(shù)據(jù), DataReader會一直占用SqlConnection連接,在其獲得數(shù)據(jù)過程中其它操作不可以再使用SqlConnection連接對象。

代碼如下:

while(datareader.read())
{
..............
}
dataview.datasource=datareader;
dataview.databind();

[2]DataSet為離線操作數(shù)據(jù),DataSet會將數(shù)據(jù)一次性讀入內(nèi)存,然后斷開連接,這時其它操作就可以使用SqlConnection連接對象。
后臺代碼如下:

public string test = "";
  protected void Page_Load(object sender, EventArgs e)
  {
    DataSet ds=new DataSet();//這里是你的數(shù)據(jù),我就不寫了
    test = "<table>";
    for (int i = 0; i < ds.Tables[0].Rows; i++)
    {
      test+="<tr><td>"+ds.Tables[0].Rows[i]["你要的字段"].ToString()+"</td></tr>"
    }
    test+="</table>";
  }
public string test = "";
  protected void Page_Load(object sender, EventArgs e)
  {
    DataSet ds=new DataSet();//這里是你的數(shù)據(jù),我就不寫了
    test = "<table>";
    for (int i = 0; i < ds.Tables[0].Rows; i++)
    {
      test+="<tr><td>"+ds.Tables[0].Rows[i]["你要的字段"].ToString()+"</td></tr>"
    }
    test+="</table>";
  }

頁面代碼如下:

<form id="form1" runat="server">
    <%=test %>
</form>

  由于DataReader一次只讀取一行數(shù)據(jù),所以占用內(nèi)存較小。但DataReader為只進且只讀的,也就是只能單方向向前讀取,如果你想回頭去讀取上一條數(shù)據(jù)是不允許的,并且不允許其修改數(shù)據(jù)。
  由于DataSet一次性讀取所有數(shù)據(jù),所以比較消耗資源,但也提高了靈活性,在斷開數(shù)據(jù)庫連接情況下你可以對數(shù)據(jù)進行任何增刪改查,按照任意的順序讀取數(shù)據(jù),并可以將其寫回到數(shù)據(jù)庫。
    有一點需要注意,DataReader一次讀取一行并不意味著這時在數(shù)據(jù)庫中的數(shù)據(jù)被修改,可以讀到新的數(shù)據(jù),這是數(shù)據(jù)庫層面的保護.

2.獲取數(shù)據(jù)的機制
  DataReader是通過IDbCommand.ExecuteReader來讀取數(shù)據(jù)。
  DataSet則是通過DbDataAdapter.Fill來填充數(shù)據(jù)。
  所以DataReader在獲取數(shù)據(jù)時不能關閉連接。而DataSet則可以,因為DbDataAdapter已經(jīng)將數(shù)據(jù)讀取到應用程序服務器中,所以在使用DataReader時一定要注意,及時關閉連接。

3.其它區(qū)別
  DataReader讀取速度快于DataSet。
  DataReader是數(shù)據(jù)提供者類,DataSet是一般性類,借助于DbDataAdapter來填充數(shù)據(jù)。
  因為DataSet是離線操作數(shù)據(jù),所以在事務中使用鎖時要注意,因為DataSet填充數(shù)據(jù)后會斷開連接,也就會釋放鎖。

4.使用DataReader可以提高執(zhí)行效率,有兩種方式可以提高代碼的性能:一種是基于序號的查找,一個是使用適當?shù)腉et方法來查找。

  因為查詢出來的結果一般都不會改變,除非再次改動查詢語句,因此可以通過定位列的位置來查找記錄。用這種方法有一個問題,就是可能知道一列的名稱而不知道其所在的位置,這個問題的解決方案是通過調(diào)用DataReader 對象的GetOrdinal()方法,此方法接收一個列名并返回此列名所在的列號。例:

代碼如下:

int id = reader.GetOrdinal("CategoryName");
while (reader.Read())
{
  Response.Write(reader.GetInt32(0).ToString() + " " + reader.GetString(1).ToString() + "<br />");
  reader.Close();
}

  DataReader的GetInt32()和GetString()通過接收一個列號來返回一個列的值,這兩種是最常用的,其中還有很多其它的類型。
  

注意:

  DataReader對象在調(diào)用其 Close() 方法關閉 DataReader 對象,然后調(diào)用 SqlConnection 對象的 Close 方法關閉與數(shù)據(jù)庫的連接,如果在沒有關閉之前又重新打開第二個連接,則會產(chǎn)生一條異常信息,如:并發(fā)問題。使用完SqlDataReader后,可以在程序中顯示的調(diào)用 數(shù)據(jù)庫連接對象的 Close() 方法關閉連接,也可以在調(diào)用Command對象的ExecuteReader方法時傳遞 CommandBehavior.CloseConnection 這個枚舉變量,這樣在調(diào)用SqlDataReader的 Close 方法時會自動關閉數(shù)據(jù)庫連接。

//未式關閉連接
        public SysFunction GetModelById(string id)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlConStr"].ToString());
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM SysFunction WHERE FunctionId =" + id, conn);
            
            SqlDataReader dataReader = cmd.ExecuteReader();
            SysFunction model = new SysFunction();
            while (dataReader.Read())
            {
                model.FunctionName = dataReader["FunctionName"].ToString();
                model.FunctionCode = dataReader["FunctionCode"].ToString();
            }

            dataReader.Close();
            dataReader.Dispose();
            string drState = dataReader.IsClosed.ToString(); //True
            string connState = conn.State.ToString(); //Open
            return model;
            
        }
        

     //調(diào)用Connection 對象的 Close() 方法顯式關閉連接
        public SysFunction GetModelById(string id)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlConStr"].ToString());
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM SysFunction WHERE FunctionId =" + id, conn);
            
            SqlDataReader dataReader = cmd.ExecuteReader();
            SysFunction model = new SysFunction();
            while (dataReader.Read())
            {
                model.FunctionName = dataReader["FunctionName"].ToString();
                model.FunctionCode = dataReader["FunctionCode"].ToString();
            }

            dataReader.Close();
            dataReader.Dispose();
       conn.Close(); //顯式關閉連接

            string drState = dataReader.IsClosed.ToString(); //True
            string connState = conn.State.ToString(); //Close
            return model;
            
        }

     //調(diào)用Command 對象的 ExecuteReader() 方法時傳遞 CommandBehavior.CloseConnection 參數(shù)
        public SysFunction GetModelById(string id)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlConStr"].ToString());
            conn.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM SysFunction WHERE FunctionId =" + id, conn);
            
            //執(zhí)行該cmd時,如果關閉關聯(lián)的 DataReader 對象,則關聯(lián)的 Connection 對象也將關閉
            SqlDataReader dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            SysFunction model = new SysFunction();
            while (dataReader.Read())
            {
                model.FunctionName = dataReader["FunctionName"].ToString();
                model.FunctionCode = dataReader["FunctionCode"].ToString();
            }

            dataReader.Close();
            dataReader.Dispose();

            string drState = dataReader.IsClosed.ToString(); //True
            string connState = conn.State.ToString(); //Close
            return model;
            
        }


手記を発表する

人気のある見出し語