ExecuteReader:连接错误消息

时间:2015-04-03 15:17:22

标签: c# asp.net executereader dbcommand

我想在我的文本框中显示数据,并且我在DAL中遇到了错误。我有一个存储过程,但我不能让我的数据返回返回值。我想使用我的BLL来返回DataTable。你能帮忙吗?

DAL

public static string GetTicket(collection b)
        {
            try
            {

                string returnValue = string.Empty;
                DB = Connect();
                DBCommand = connection.Procedure("getTicket");
                DB.AddInParameter(DBCommand, "@SupportRef", DbType.String, b.SupportRef1);

                var myReader = DBCommand.ExecuteReader();
                while (myReader.Read())
                {
                    returnValue = myReader.GetString(0);
                }

                return returnValue;
            }

            catch (Exception ex)
            {
                throw ex;
            }

        }

我收到以下错误:

ExecuteReader: Connection property has not been initialized.

我使用的连接类看起来像这样:

public class connection
{
    const string StrConnection = "TicketHelperConnectionString";
    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);            
        }
    }
}

1 个答案:

答案 0 :(得分:0)

看起来未设置DBCommand的Connection属性,在从Procedure方法返回命令之前,必须设置其连接属性:

DBCommand = DB.GetStoredProcCommand(procedure);

// DBCommand.Connection = ...

return DBCommand;