DataSet,DataReader,DataTable关系

来源:互联网 发布:实用的网络运营方案 编辑:程序博客网 时间:2024/05/29 04:40

1.简单说就是

 DataSet:数据集。一般包含多个DataTable,用的时候,dataset["表名"]得到DataTable  
   
  DataTable:数据表。  
  一:  
  SqlDataAdapter da=new SqlDataAdapter(cmd);  
  DataTable dt=new DataTable();  
  da.Fill(dt);  
   
  -----------------  
  直接把数据结果放到datatable中,  
  二:  
  SqlDataAdapter da=new SqlDataAdapter(cmd);  
  DataSet dt=new DataSet();  
  da.Fill(dt);  
  ----------------  
  数据结果放到dataset中,若要用那个datatable,可以这样:dataset[0]  
  更常见的用法:  
  SqlDataAdapter da=new SqlDataAdapter(cmd);  
  DataSet dt=new DataSet();  
  da.Fill(dt,"table1");  
  用的时候:这样取datatable:  
  dataset["table1"]  
   


DataSet不能直接用来存储数据,如果仅用DataSet,它会自动的生成一个DataTable,所以看上去就像一个DataTable。  
   
  如果有很多个DataTable需要同时绑定到某个控件,就可以把多个DataTable添加到一个DataSet中,分别起不同的名字就可以了Top
DataReader基于连接,它返回的数据是只读只向前的,适合简单地浏览且耗时比较短的操作。DataSet对象会将所需数据读入内存然后断开连接。它适合对数据进行复杂长时间的操作,并且需要更新数据的情况。Top

SqlDataReader只是建立与数据库之间的类似于一个指针关系,在没有调用Read()方法之间它不从数据读出任何数据,而在调用Read()时也只是从数据库中读出一条数据.DATATABLE是数据表的一个复本,至于DataSet可以反它看作一个数据库,因为它不但可以包括多个表而且还能包括各表之间的关系.所以在进行大量数据访问时(特别是分页方式的访问)建议使用SqlDataReader,这样可以节省大量的内存开销.Top

DataSet就像内存数据库,  
  DataTable就是表,  
  库可以包含多个表,也可以包含表之间的关系。

 

2.DataReader DataTable DataSet 之间的转换

绑定Gridview里往往数据源是DataSet 或是DataTable 嗯 ,一些类库(SQLHelper等)里面的方法返回的是DataReader嗯 ,怎么把它们转成DataSet呢?

(1)
DataReader转为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)

注:DataAdapter与DataReader是不同的哦

DataAdapter可以这样做:

DataAdapter.Fill(ds)
(3)
#region DataReader转换为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("转换出错!", ex);
            }
        }
        #endregion
http://space.itpub.net/14466241/viewspace-545525
http://didasoft.javaeye.com/blog/274670
http://www.cnblogs.com/phoenix-rock/archive/2006/10/31/interoperability-recordset-dataset.html
DataTable缓存数据操作:
http://space.itpub.net/12639172/viewspace-526838
如何将SqlDataReader绑定到DataGrid
http://smilecong.blog.51cto.com/121860/21972
C#中提供的精准测试程序运行时间的类Stopwatch
http://blog.csdn.net/genghuilove/archive/2008/11/21/3345638.aspx
c#中连接数据库SqlDataAdapter的用法
http://bbs.pfan.cn/showtxt.asp?id=106678
http://www.cnblogs.com/lvhaitao/archive/2006/12/25/602912.html
http://www.programfan.com/blog/article.asp?id=30259   DataReader用法
SqlDataAdapter它的用法有很多,比DataReader强大多了,感兴趣的朋友可以查查。DataReader是只读的,也就是单向的。而适配器呢,它既可以读又可以写。
读取Excel内容,导入数据库多张表!

 

 

原创粉丝点击