dimanche 19 avril 2015

how to initialise connection property within ExecuteReader

I would like to know based on my code, how i can initialise the connection property into my ExecuteReader correctly?


Here is my connection class:



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);
}
}
}
}


I am getting an error:



ExecuteReader: Connection property has not been initialized.


I have the following:



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);
}

}

}
}


How do I pass the connection into:



DbDataReader reader = DBCommand.ExecuteReader();

Aucun commentaire:

Enregistrer un commentaire