【问题标题】:ExecuteReader requires an open and available Connection Asp.netExecuteReader 需要一个开放且可用的连接 Asp.net
【发布时间】:2014-01-27 21:21:28
【问题描述】:

我的网站在服务器上时出现错误。

System.Data ExecuteReader 需要一个打开且可用的连接。 连接的当前状态是打开的

private static int OpenCon()
    {
        int degel = 0;
        try
        {
            ResetCon(true);
            if (con.State != ConnectionState.Open)
                con.Open();
            degel = -1;
        }
    catch (System.Data.SqlClient.SqlException ex)
    {
        CloseCon();
        if (ex.Errors[0].Number == 53)
        {

        }
    }
    return degel;
}

private static void CloseCon()
{
    if (con.State != ConnectionState.Closed)
        con.Close();
    if (cmd == null) 
        return; 
    else 
        cmd.Dispose();
}

private static SqlCommand CreateCmd(string sql, CommandType type)
{
    SqlCommand cmd = new SqlCommand();
    try
    {
        cmd.Connection = con;
        cmd.CommandType = type;
        cmd.CommandText = sql;
        cmd.CommandTimeout = 300;
    }
    catch
    {
        CloseCon();
    }
    return cmd;
}

 public static DataTable GetTable(string sql)
        {
            DataTable dt = new DataTable();
            try
            {
                OpenCon();
                SqlCommand cmd = CreateCmd(sql, CommandType.Text);
                adpt = new SqlDataAdapter();
                adpt.SelectCommand = cmd;
                adpt.Fill(dt);
                CloseCon();
            }
            catch (Exception ex)
            {
                CloseCon();
                Log.WriteLog("DataBase.GetTable()", ex, HttpContext.Current.Request);
            }
            return dt;
        }

谢谢, 哈尼

【问题讨论】:

    标签: asp.net database database-connection


    【解决方案1】:

    我认为此错误在您的开发 PC 上无法重现,是吗?

    您不应该使用静态连接,在 ASP.NET 中更是如此。您的 DB 类只是像这样令人讨厌的错误的来源。而是对实现IDisposable 的所有内容(例如连接)使用using 语句,以确保它尽快关闭(即使出现错误)。

    我不知道我可以添加什么,因为这似乎与 ...

    【讨论】:

      猜你喜欢
      • 2014-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多