什么是数据读取器和数据适配器? [英] what is datareader and data adpater?

查看:145
本文介绍了什么是数据读取器和数据适配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是数据读取器和数据适配器?建议使用那些读取器和适配器吗?
请用一个简单的例子来解释吗?
若要创建SqlDataReader,必须调用SqlCommand对象的ExecuteReader方法,而不是直接使用构造函数.

在使用SqlDataReader时,关联的SqlConnection忙于为SqlDataReader提供服务,除了关闭SqlConnection之外,无法对SqlConnection进行任何其他操作.在调用SqlDataReader的Close方法之前,情况就是这样.例如,只有在调用Close之后才能检索输出参数.
示例:

 使用系统;
使用 System.Data;
使用 System.Data.SqlClient;


班级计划
{
    静态void Main()
    {
        字符串str = " 
            + " ;
        ReadOrderData(str);
    }

    私有静态无效ReadOrderData(string connectionString)
    {
        字符串queryString =
            " ;

        使用(SqlConnection connection =
                   新的SqlConnection(connectionString))
        {
            SqlCommand命令=
                新的SqlCommand(queryString,connection);
            连接.打开();

            SqlDataReader reader = command.ExecuteReader();

            //在访问数据之前,调用读取.
            同时(阅读器.阅读())
            {
                ReadSingleRow((IDataRecord)reader);
            }

            //阅读完毕后,调用关闭  .
            阅读器.关闭();
        }
    }

    私有静态无效ReadSingleRow(IDataRecord记录)
    {
        Console.WriteLine(String.Format(" ,记录[ 0 ],记录[ 1 ])));
    }

} 



数据适配器:
SqlDataAdapter充当DataSet和SQL Server之间的桥梁,用于检索和保存数据. SqlDataAdapter通过映射Fill(使用适当的Transact-SQL来映射DataSet中的数据以匹配数据源中的数据)和Update(通过更改数据源中的数据以匹配DataSet中的数据)来提供此桥.针对数据源的语句.该更新是逐行执行的.对于每个插入,修改和删除的行,Update方法确定对其执行的更改的类型(插入,更新或删除).根据更改的类型,执行插入",更新"或删除"命令模板,以将修改后的行传播到数据源.当SqlDataAdapter填充数据集时,如果返回的数据尚不存在,它将为返回的数据创建必要的表和列.但是,除非将MissingSchemaAction属性设置为AddWithKey,否则隐式创建的架构中不包含主键信息.在使用FillSchema向其中填充数据之前,还可以让SqlDataAdapter创建DataSet的架构,包括主键信息.有关更多信息,请参见将现有约束添加到数据集(ADO.NET).

SqlDataAdapter与SqlConnection和SqlCommand结合使用,以提高连接到SQL Server数据库时的性能.

示例:

 私有 静态 DataSet SelectRows(DataSet数据集,
    字符串 connectionString,字符串 queryString)
{
    使用(SqlConnection connection =
         SqlConnection(connectionString))
    {
        SqlDataAdapter adapter =  SqlDataAdapter();
        adapter.SelectCommand =  SqlCommand(
            queryString,连接);
        adapter.Fill(数据集);
        返回数据集;
    }
} 


请参阅以下链接

http://msdn.microsoft.com/en-us/library/ms254931.aspx [ ^ ]


what is datareader and data adpater? waht is propose of using those reader and adapter?
pls explain with simple example????

解决方案

DataReader:

To create a SqlDataReader, you must call the ExecuteReader method of the SqlCommand object, instead of directly using a constructor.

While the SqlDataReader is being used, the associated SqlConnection is busy serving the SqlDataReader, and no other operations can be performed on the SqlConnection other than closing it. This is the case until the Close method of the SqlDataReader is called. For example, you cannot retrieve output parameters until after you call Close.
Example:

using System;
using System.Data;
using System.Data.SqlClient;


class Program
{
    static void Main()
    {
        string str = "Data Source=(local);Initial Catalog=Northwind;"
            + "Integrated Security=SSPI";
        ReadOrderData(str);
    }

    private static void ReadOrderData(string connectionString)
    {
        string queryString =
            "SELECT OrderID, CustomerID FROM dbo.Orders;";

        using (SqlConnection connection =
                   new SqlConnection(connectionString))
        {
            SqlCommand command =
                new SqlCommand(queryString, connection);
            connection.Open();

            SqlDataReader reader = command.ExecuteReader();

            // Call Read before accessing data. 
            while (reader.Read())
            {
                ReadSingleRow((IDataRecord)reader);
            }

            // Call Close when done reading.
            reader.Close();
        }
    }

    private static void ReadSingleRow(IDataRecord record)
    {
        Console.WriteLine(String.Format("{0}, {1}", record[0], record[1]));
    }

}



DataAdapter:
The SqlDataAdapter, serves as a bridge between a DataSet and SQL Server for retrieving and saving data. The SqlDataAdapter provides this bridge by mapping Fill, which changes the data in the DataSet to match the data in the data source, and Update, which changes the data in the data source to match the data in the DataSet, using the appropriate Transact-SQL statements against the data source. The update is performed on a by-row basis. For every inserted, modified, and deleted row, the Update method determines the type of change that has been performed on it (Insert, Update, or Delete). Depending on the type of change, the Insert, Update, or Delete command template executes to propagate the modified row to the data source. When the SqlDataAdapter fills a DataSet, it creates the necessary tables and columns for the returned data if they do not already exist. However, primary key information is not included in the implicitly created schema unless the MissingSchemaAction property is set to AddWithKey. You may also have the SqlDataAdapter create the schema of the DataSet, including primary key information, before filling it with data using FillSchema. For more information, see Adding Existing Constraints to a DataSet (ADO.NET).

SqlDataAdapter is used in conjunction with SqlConnection and SqlCommand to increase performance when connecting to a SQL Server database.

Example:

private static DataSet SelectRows(DataSet dataset,
    string connectionString,string queryString)
{
    using (SqlConnection connection =
        new SqlConnection(connectionString))
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(
            queryString, connection);
        adapter.Fill(dataset);
        return dataset;
    }
}


Refer below link

http://msdn.microsoft.com/en-us/library/ms254931.aspx[^]


这篇关于什么是数据读取器和数据适配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆