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

分享

數(shù)據(jù)集處理技術(shù) DataReader DataTable DataSet 之間的轉(zhuǎn)換

 青格兒 2009-03-19
      
http://www.cnblogs.com/binarytree/archive/2008/10/09/1306843.html

       綁定Gridview里往往數(shù)據(jù)源是DataSet 或是DataTable 嗯 ,一些類庫(SQLHelper等)里面的方法返回的是DataReader嗯 ,怎么把它們轉(zhuǎn)成DataSet呢?

(1)

DataReader轉(zhuǎn)為DataSet的類:

 

private   DataSet   DataReaderToDataSet(IDataReader   reader)    

  {    

  DataTable   table   =   new   DataTable();    

  int   fieldCount   =   reader.FieldCount;     

  for   (int   i   =   0;   i   <   fieldCount;   i++)    

  {    

  table.Columns.Add(reader.GetName(i),   reader.GetFieldType(i));    

  }    

  table.BeginLoadData();    

  object[]   values   =   new   object[fieldCount];    

  while   (reader.Read())    

  {    

  reader.GetValues(values);    

  table.LoadDataRow(values,   true);    

  }    

  table.EndLoadData();    

  DataSet   ds   =   new   DataSet();    

  ds.Tables.Add(table);    

  return   ds;    

  }

 

(2)

:DataAdapterDataReader是不同的哦

DataAdapter可以這樣做:

DataAdapter.Fill(ds)
 
(3)
#region DataReader轉(zhuǎn)換為DataTable
  
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static DataTable ConvertDataReaderToDataTable(SqlDataReader reader)
        {
            try
            {
                DataTable objDataTable = new DataTable();
                int intFieldCount = reader.FieldCount;
                for (int intCounter = 0; intCounter < intFieldCount; ++intCounter)
                {
                    objDataTable.Columns.Add(reader.GetName(intCounter), reader.GetFieldType(intCounter));
                }

                objDataTable.BeginLoadData();

                object[] objValues = new object[intFieldCount];
                while (reader.Read())
                {
                    reader.GetValues(objValues);
                    objDataTable.LoadDataRow(objValues, true);
                }
                reader.Close();
                objDataTable.EndLoadData();

                return objDataTable;

            }
            catch (Exception ex)
            {
                throw new Exception("轉(zhuǎn)換出錯(cuò)!", ex);
            }
        }
        #endregion
 
DataTable緩存數(shù)據(jù)操作:
 
 
如何將SqlDataReader綁定到DataGrid
 
 
C#中提供的精準(zhǔn)測(cè)試程序運(yùn)行時(shí)間的類Stopwatch
 
 
c#中連接數(shù)據(jù)庫SqlDataAdapter的用法
 
 
SqlDataAdapter它的用法有很多,比DataReader強(qiáng)大多了,感興趣的朋友可以查查。DataReader是只讀的,也就是單向的。而適配器呢,它既可以讀又可以寫。
 
 
讀取Excel內(nèi)容,導(dǎo)入數(shù)據(jù)庫多張表!
 
 

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

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多