ExecuteReader:连接错误消息



我想在文本框中显示数据,但我在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);            
        }
    }
}

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

DBCommand = DB.GetStoredProcCommand(procedure);
// DBCommand.Connection = ...
return DBCommand;

相关内容

  • 没有找到相关文章

最新更新