asp.net中读取数据库的两种方式

在asp.net中,读取数据库中的数据可以使用DataReader和DataSet 2种方式,
两者的差异如下:
使用Dataset对象读取数据大致需要以下5个步骤:
(1)建立数据库链接,可以选用SQLConnection或者01edbConnection。
(2)将查询保存在SQLDataAdapter或者oledbDataAdapter对象中。
(3)使用DataAdapter对象的Fill方法将数据填充到DataSet中的DataTable中。
(4)为DataTable对象建立DataView对象以显示数据,这一步可以省略。
(5)将DataView或者DataSet绑定到Server Control上。

 

使用DataReader读取数据大致需要以下6个步骤:
(1)建立数据库链接,可以选SQLConnection或者OLedbConnection。
(2)使用Connection对象的open方法打开数据库链接。
(3)将查询保存在SQLCommand或者OledbCommand对象中。
(4)调用Command对象的ExecuteReader方法,将数据读入DataReader对象中。
(5)调用DataReader的Read或者Get方法读取—笔数据,以便显不。
(6)调用Connection对象的Close方法,关闭数据序链接。

第一种方法我们已经用过很多次了,这里就不在详细介绍了

下面我们来看看第二种方法的使用


OracleCommand ocmd = new OracleCommand("select id,fldname,caption,fldType,maxLength,relateTbl,relateFld,nvl(bNull,1) from S_Fields where tblname='" + name + "'", ocon);
            OracleDataReader oda = ocmd.ExecuteReader();
            if (oda.Read())
            {
                this.TextBox1.Text = Convert.ToString(oda["caption"]);
            }
           
            ocon.Close();
原文地址:https://www.cnblogs.com/gergro/p/367295.html