如何在ExecuteReader中初始化连接属性

时间:2015-04-19 12:53:02

标签: c# asp.net webforms data-access-layer executereader

我想知道基于我的代码,我如何正确地将连接属性初始化为我的ExecuteReader?

这是我的连接类

namespace DAL
{
    public class connection
    {
        const string StrConnection = "CustomerHelperConnectionString";
        internal static Database DB;
        public static DbCommand DBCommand;
        public static Database Connect()
        {

            try
            {
                DB = DatabaseFactory.CreateDatabase(StrConnection);
                return DB;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
        public static DbCommand Procedure(string procedure)
        {

            try
            {
                DBCommand = DB.GetStoredProcCommand(procedure);
                return DBCommand;
            }
            catch (Exception ex)
            {
                throw (ex);            
            }
        }
    }
}

我收到错误:

ExecuteReader: Connection property has not been initialized.

我有以下内容:

public static DataTable GetCustomer(string CustomerRef1)
{
    {
        {
            DataTable table;
            try
            {
                string returnValue = string.Empty;

                DB = Connect();
                DBCommand = connection.Procedure("getCustomer);
                DB.AddInParameter(DBCommand, "@CustomerRef", DbType.String, CustomerRef1);


                DbDataReader reader = DBCommand.ExecuteReader(DBCommand.Connection("StrConnection));
                table = new DataTable();
                table.Load(reader);
                return table;
            }
            catch (Exception ex)
            {
                throw (ex);
            }

        }

    }
}

如何将连接传递到:

DbDataReader reader = DBCommand.ExecuteReader();

0 个答案:

没有答案