Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 连接关闭错误_Asp.net_Sql Server 2008 R2 - Fatal编程技术网

Asp.net 连接关闭错误

Asp.net 连接关闭错误,asp.net,sql-server-2008-r2,Asp.net,Sql Server 2008 R2,我在这段代码中有两个DataReader,vs给我这个错误“连接没有关闭。连接的当前状态是打开的。”错误来自connection2。我查看了我的代码,但我的两个连接已关闭。问题在哪里 protected void Page_Load(object sender, EventArgs e) { using (SqlConnection connection1 = DBConnection.getConnection()) { string strquer

我在这段代码中有两个DataReader,vs给我这个错误“连接没有关闭。连接的当前状态是打开的。”错误来自connection2。我查看了我的代码,但我的两个连接已关闭。问题在哪里

     protected void Page_Load(object sender, EventArgs e)
{

    using (SqlConnection connection1 = DBConnection.getConnection())
    {
        string strquery1 = " with distinctvalueyes (typearticle) as (select top 1 'Fruit' FROM WeedingSalonGeneralRes where Fruit=1  union all select top 1 'Drink' FROM WeedingSalonGeneralRes where Drink=1 union all select top 1 'Desert' FROM WeedingSalonGeneralRes where Desert=1  union all select top 1 'MainFood' FROM WeedingSalonGeneralRes where MainFood=1 union all select top 1 'Salad' FROM WeedingSalonGeneralRes where Salad=1 union all select top 1 'TableFlower' FROM WeedingSalonGeneralRes where TableFlower=1 union all  select top 1 'SaloonLighting' FROM WeedingSalonGeneralRes where SaloonLighting=1 union all  select top 1 'Saloondesign' FROM WeedingSalonGeneralRes where Saloondesign=1 union all select top 1 'SloonCrew' FROM WeedingSalonGeneralRes where SloonCrew=1  union all  select top 1 'Pastry' FROM WeedingSalonGeneralRes where Pastry=1 union all  select top 1 'GiftCard' FROM WeedingSalonGeneralRes where GiftCard=1 )  select * from distinctvalueyes ";
        connection1.Open();
        SqlCommand cmd1 = new SqlCommand();
        cmd1.Connection = connection1;
        cmd1.CommandText = strquery1;
        string cis = Session["customerID"].ToString();
        lbl2_customerid.Text = cis;

        SqlDataReader reader = cmd1.ExecuteReader();
        if (reader.Read())
        {
            lbl8_fruit.Text = reader[0].ToString();

            // wrapped in a using block, connection will now always be closed and disposed
            using (SqlConnection connection2 = DBConnection.getConnection())
            {
                //query for fetch service prices 
                string strquery2 = "SELECT Fruit_price,Drink_price,Desert_price,MainFood_price,Salad_price,TableFlower_price,SaloonLighting_price,SaloonDesign_price,SaloonCrew_price,Pastry_price,GiftCard_price  FROM GenReservationServicePrice";
                connection2.Open();
                SqlCommand cmd2 = new SqlCommand();
                cmd2.Connection = connection2;
                cmd2.CommandText = strquery2;
                SqlDataReader reader2 = cmd2.ExecuteReader();
                if (reader2.Read())
                {
                    string Fruit_price;
                    string Drink_price;
                    string Desert_price;
                    string MainFood_price;
                    string Salad_price;
                    string TableFlower_price;
                    string SaloonLighting_price;
                    string SaloonDesign_price;
                    string SaloonCrew_price;
                    string Pastry_price;
                    string GiftCard_price;


                    Fruit_price = reader[0].ToString();
                    Drink_price = reader[1].ToString();
                    Desert_price = reader[2].ToString();
                    MainFood_price = reader[3].ToString();
                    Salad_price = reader[4].ToString();
                    TableFlower_price = reader[5].ToString();
                    SaloonLighting_price = reader[6].ToString();
                    SaloonDesign_price = reader[7].ToString();
                    SaloonCrew_price = reader[8].ToString();
                    Pastry_price = reader[9].ToString();
                    GiftCard_price = reader[10].ToString();


                    lbl8_fruit.Text = Fruit_price;

                }
            }
        }
    }
}



///connection class 
public class DBConnection
{
 private static SqlConnection connection=null;
 public DBConnection()
{

}

public static SqlConnection getConnection()
{
    if (connection != null)
    { return connection; }
    else
    {
        String connectionString = WebConfigurationManager.AppSettings["connectionString"];
        connection = new SqlConnection(connectionString);
        return connection;
    }
}
}

编辑 问题是您试图在两个位置使用相同的
SqlConnection
实例。每次需要时,都应该使用新的
SqlConnection

  • 创造它
  • 使用它
  • 处理它
代码更改:

///connection class 
public class DBConnection
{
    public static SqlConnection getConnection()
    {
        String connectionString = WebConfigurationManager.AppSettings["connectionString"];
        return new SqlConnection(connectionString);
    }
}
Sql Server有一种称为连接池的功能,默认情况下会启用这种功能。在c#中创建连接将重用Sql Server上已经可用的连接,因此此操作非常便宜。将所有使用
SqlConnections
的代码包装在
using
块中,以确保它们始终处于关闭状态,从而不会发生连接泄漏

此外,app.config/web.config还有一个。你应该使用它,而不是应用程序设置


使用SqlConnection 您应该使用
块将所有
SqlConnection
实例包装在
中。这将确保即使发生异常,连接也始终处于关闭和释放状态。它还需要对代码执行所有检查工作,无需查看代码是否在关闭时打开

这是您使用
块更改的代码,我删除了您的一些逻辑,所以很清楚我更改了什么

protected void Page_Load(object sender, EventArgs e)
{
    /* your existing code removed for clarit */

    // wrapped in a using block, connection will now always be closed and disposed
    using(SqlConnection connection1 = DBConnection.getConnection())
    {
        connection1.Open();
        SqlCommand cmd1 = new SqlCommand();
        cmd1.Connection = connection1;
        cmd1.CommandText = strquery1;
        string cis = Session["customerID"].ToString();
        lbl2_customerid.Text = cis;

        SqlDataReader reader = cmd1.ExecuteReader();
        if (reader.Read())
        {
            /* your existing code removed for clarit */

            // wrapped in a using block, connection will now always be closed and disposed
            using(SqlConnection connection2 = DBConnection.getConnection())
            {
                connection2.Open();
                SqlCommand cmd2 = new SqlCommand();
                cmd2.Connection = connection2;
                cmd2.CommandText = strquery2;
                SqlDataReader reader2 = cmd2.ExecuteReader();
                if (reader2.Read())
                {
                    /* your existing code removed for clarit */
                }
            }
        }
    }
}
编辑 问题是您试图在两个位置使用相同的
SqlConnection
实例。每次需要时,都应该使用新的
SqlConnection

  • 创造它
  • 使用它
  • 处理它
代码更改:

///connection class 
public class DBConnection
{
    public static SqlConnection getConnection()
    {
        String connectionString = WebConfigurationManager.AppSettings["connectionString"];
        return new SqlConnection(connectionString);
    }
}
Sql Server有一种称为连接池的功能,默认情况下会启用这种功能。在c#中创建连接将重用Sql Server上已经可用的连接,因此此操作非常便宜。将所有使用
SqlConnections
的代码包装在
using
块中,以确保它们始终处于关闭状态,从而不会发生连接泄漏

此外,app.config/web.config还有一个。你应该使用它,而不是应用程序设置


使用SqlConnection 您应该使用
块将所有
SqlConnection
实例包装在
中。这将确保即使发生异常,连接也始终处于关闭和释放状态。它还需要对代码执行所有检查工作,无需查看代码是否在关闭时打开

这是您使用
块更改的代码,我删除了您的一些逻辑,所以很清楚我更改了什么

protected void Page_Load(object sender, EventArgs e)
{
    /* your existing code removed for clarit */

    // wrapped in a using block, connection will now always be closed and disposed
    using(SqlConnection connection1 = DBConnection.getConnection())
    {
        connection1.Open();
        SqlCommand cmd1 = new SqlCommand();
        cmd1.Connection = connection1;
        cmd1.CommandText = strquery1;
        string cis = Session["customerID"].ToString();
        lbl2_customerid.Text = cis;

        SqlDataReader reader = cmd1.ExecuteReader();
        if (reader.Read())
        {
            /* your existing code removed for clarit */

            // wrapped in a using block, connection will now always be closed and disposed
            using(SqlConnection connection2 = DBConnection.getConnection())
            {
                connection2.Open();
                SqlCommand cmd2 = new SqlCommand();
                cmd2.Connection = connection2;
                cmd2.CommandText = strquery2;
                SqlDataReader reader2 = cmd2.ExecuteReader();
                if (reader2.Read())
                {
                    /* your existing code removed for clarit */
                }
            }
        }
    }
}

获取连接2.Close();在if语句之外。我移动connection2.Close();在if语句之外,但我再次遇到了错误,请尝试封闭从connection2.Open()开始的第二个块;在使用块中;使用(SqlConnection connection2=DBConnection.getConnection()){connection2.Open();…}并删除connection2.Close();你的意思是我使用一个连接所有人?你不是从不同的数据库中提取数据,对吗。所以,是的,所有人都有一个连接。我会把if语句改为while语句。因此,代替if(reader.Read()){..},而(reader.Read()){..}获取connection2.Close();在if语句之外。我移动connection2.Close();在if语句之外,但我再次遇到了错误,请尝试封闭从connection2.Open()开始的第二个块;在使用块中;使用(SqlConnection connection2=DBConnection.getConnection()){connection2.Open();…}并删除connection2.Close();你的意思是我使用一个连接所有人?你不是从不同的数据库中提取数据,对吗。所以,是的,所有人都有一个连接。我会把if语句改为while语句。因此,我使用这些代码进行测试,而不是if(reader.Read()){..}而(reader.Read()){..}时出现了错误。我通过一步一步地调试和连接2.Open()进行测试;错误是shown@meysam-DBConnection.getConnection的作用是什么?你能提供那个代码吗?如果它每次都返回一个静态连接实例(即同一个实例),那么这就是问题所在,就像您试图更改已经打开的连接的状态一样。我使用这些代码进行测试,我发现了错误。我通过一步一步地调试和连接2.Open()进行测试;错误是shown@meysam-DBConnection.getConnection的作用是什么?你能提供那个代码吗?如果它每次都返回一个静态连接实例(即同一个实例),那么这就是问题所在,就像您试图更改已打开连接的状态一样。