ADO.NET DAL和BLL

时间:2015-11-25 18:46:50

标签: c#

我有两个课程SqlHelperDishesTypes在DAL项目中使用

public class SqlHelper
{ 
    public static SqlDataReader ExecuteReader(string procedure, 
        params SqlParameter[] commandParameters) 
    {             
        using (var connection = new SqlConnection(
            ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))       
        using (var command = new SqlCommand(procedure, _connection)) 
        {                    
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddRange(commandParameters);
            return command.ExecuteReader();                                 
        }    
    }

    public class DishesTypes 
    {
        public static SqlDataReader DishesTypesSelectAll() 
        {
            return SqlHelper.ExecuteReader("DishesTypesSelectAllRows"); //name of procedure
        }
    }

我在这样的BLL​​项目中使用了课程DishedTypes

public class DishesTypes
{
    public int DishTypeId { get; set; }
    public string DishType { get; set; }

    public static List<DishesTypes> DishesTypesSelectAll()
    {
        IDataReader dr = DataAccessLayer.DishesTypes.DishesTypesSelectAll();          

        List<DishesTypes> dishesTypesList = new List<DishesTypes>();

        while (dr.Read())
        {
            DishesTypes myDishesTypes = new DishesTypes
            {
                DishTypeId = (int)dr["DishTypeId"],
                DishType = (string)dr["DishType"]
            };
            dishesTypesList.Add(myDishesTypes);
        }
        return dishesTypesList;
    }
}

问题从这里开始while (dr.Read()),原因是,与此点的连接已经关闭,有必要重新连接如何最好地更改粘附DAL和BLL的类的实现,以便工作?

2 个答案:

答案 0 :(得分:1)

如果你想自己动手,这样的事情会更好:

public class DataQuery
{
    private readonly string _connectionString;

    public DataQuery(string connectionString)
    {
        _connectionString = connectionString;
    }

    public IEnumerable<T> GetList<T>(string procedure,
        Func<IDataRecord, T> entityCreator,
        params SqlParameter[] commandParameters
        )
    {
        var result = new List<T>();
        using (var connection = CreateConnection())
        using (var command = CreateCommand(procedure, connection, commandParameters))
        using (var reader = command.ExecuteReader())
        {
            result.Add(entityCreator(reader));
        }
        return result;
    }

    private SqlConnection CreateConnection()
    {
        var connection = new SqlConnection(_connectionString);
        connection.Open();
        return connection;
    }

    private static DbCommand CreateCommand(string procedure, 
        SqlConnection connection, SqlParameter[] commandParameters)
    {
        var command = new SqlCommand(procedure, connection)
        {
            CommandType = CommandType.StoredProcedure
        };
        command.Parameters.AddRange(commandParameters);
        return command;
    }
}

你会这样称呼:

var connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"]
    .ConnectionString;

var query = new DataQuery(connectionString);
Func<IDataRecord, DishesTypes> creator = dr => 
    new DishesTypes
    {
        DishTypeId = (int)dr["DishTypeId"],
        DishType = (string)dr["DishType"]
    };

var results = query.GetList("DishesTypesSelectAllRows", creator);

否则,我建议您查看http://dev.mysql.com/doc/refman/5.7/en/cursor-restrictions.html

Dapper会让你简单地做:

var results = connection.Query<DishesTypes>("DishesTypesSelectAllRows", 
    commandType: CommandType.StoredProcedure);

答案 1 :(得分:0)

首先,您的using语句正在关闭您的连接,因此您不能指望返回可用的IDataReader。其次,您的连接永远不会打开,所以无论如何都不会得到结果。话虽如此,如果你的数据集总是足够小以适应内存,你可以使用类似我在下面所做的事情。这应该对您的代码影响最小。

    public class SqlHelper
    {
        public static IDataReader ExecuteReader(string procedure, params SqlParameter[] commandParameters)
        {
            using (var connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
            {
                connection.Open();
                using (var command = new SqlCommand(procedure, connection))
                {
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.AddRange(commandParameters);
                    DataTable dt = new DataTable();
                    using (SqlDataAdapter da = new SqlDataAdapter(command))
                        da.Fill(dt);
                    return dt.CreateDataReader();
                }
            }
        }
    }

    public class DishesTypes
    {
        public static IDataReader DishesTypesSelectAll()
        {
            return SqlHelper.ExecuteReader("DishesTypesSelectAllRows");//name of procedure
        }

    }