如何在DAL中使用sqldatareader? [英] how to use sqldatareader in DAL?

查看:64
本文介绍了如何在DAL中使用sqldatareader?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用存储过程检索数据并将其传递到表示层中的下拉列表

但问题是如何使用sqldatareader

i'm try to retrieve data using stored procedure and pass it to dropdownlist in presentation layer
but the problem is how to this using sqldatareader

推荐答案

        public static getrecords()
{
            DataTable tbl = null;
            SqlConnection conn = null;
            try
            {
                conn = "Your Connection String";
                cmd.Connection = conn;
                tbl = new DataTable();
                SqlDataReader dr = cmd.ExecuteReader();
                tbl.Load(dr);

                dr.Close();
                dr.Dispose();
            }
            catch (Exception ex)
            {
                LogFile.WriteLog(ex.ToString());
                throw ex;
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
            return tbl;
}





这样你可以使用它。



THis way you can use it.


参考:

1. SqlDataReader类 [ ^ ]

2. 使用SqlDataReader读取数据 [ ^ ]



+++ +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -layer-using-sqldatareader-and -c> data-access-layer-using-sqldatareader [ ^ ]
Refer:
1. SqlDataReader Class[^]
2. Reading Data with the SqlDataReader[^]

++++++++++++++
[added]
data-access-layer-using-sqldatareader[^]


SqlDataReader可能不是最好的选择在DAL中使用。原因是它不像其他物体那样可以传递。你可以传递一个datareader,但你必须继续调用.Read()来获取下一行,它不是一次性的整个数据集。在我看来,DataSet或DataTables对于DAL更好。



但是,DAL是你想要的任何东西。您正在编写代码,因此负责它的外观,以便让它适合您。
A SqlDataReader may not be the best choice for use in a DAL. The reason being that it is not like other objects that you can pass around. You can pass a datareader but you have to keep calling .Read() on it to get the next rows, it is not the whole set of data in one shot. DataSets or DataTables are better for DAL, in my opinion.

However, a DAL is whatever you want it to be. You are writing the code and are therefore in charge of what it looks like so make it work for you.


这篇关于如何在DAL中使用sqldatareader?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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