首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

ExecuteScalar 要求已打开且可用的连接。连接的当前状态为已关闭,该怎么解决

2012-02-03 
ExecuteScalar 要求已打开且可用的连接。连接的当前状态为已关闭问题是:我已经在每次用完DataReader后,都把

ExecuteScalar 要求已打开且可用的连接。连接的当前状态为已关闭
问题是:我已经在每次用完DataReader后,都把对应的DataReader关闭了,同时他们共用一个SqlConnection,不过我每次用时,都把CommandText改过来了,可是还是出现这个错误提示,请高手指点。
this.Exsql.Open();
                        string   cmdText   =   " ";
                        string   text2   =   " ";
                        string   text3   =   " ";
                        string   text4   =   " ";
                        string   text5   =   " ";
                        cmdText   =   "select   DISTINCT   YX_ProID   from   YX_Shoping ";
                        SqlCommand   com   =   new   SqlCommand(cmdText,   this.Exsql.con);
                        SqlDataReader   reader   =   com.ExecuteReader();
                        DataSet   set   =   new   DataSet();
                        DataTable   table   =   new   DataTable( "tab1 ");
                        set.Tables.Add(table);
                        DataColumn   column   =   new   DataColumn( "YX_ID ",   Type.GetType( "System.Int32 "));
                        set.Tables[ "tab1 "].Columns.Add(column);
                        column   =   new   DataColumn( "YX_ProName ",   Type.GetType( "System.String "));
                        set.Tables[ "tab1 "].Columns.Add(column);
                        column   =   new   DataColumn( "YX_ProNum ",   Type.GetType( "System.Int32 "));
                        column.DefaultValue   =   0;
                        set.Tables[ "tab1 "].Columns.Add(column);
                        column   =   new   DataColumn( "YX_ProPic ",   Type.GetType( "System.String "));
                        set.Tables[ "tab1 "].Columns.Add(column);

                        int   Lenreader   =   -1;
                        Hashtable   hashtable   =   new   Hashtable();
                       


                        while   (reader.Read())
                        {
                                Lenreader++;      
                                hashtable.Add(Lenreader,   reader[0].ToString());
                                           
                        }
                        reader.Close();

                        for   (int   i   =   0;   i   <=   Lenreader;i++   )
                        {
                                com.CommandText= "Select   sum(YX_OrderProNum)   from   YX_Shoping   where   YX_ProID= ' "   +   hashtable[i].ToString()   +   " ' ";
                                int   num   =   Convert.ToInt32(com.ExecuteScalar());
                                com.CommandText= "Select   YX_ProID,YX_ProName   from   YX_Shoping   where   YX_ProID= ' "   +   hashtable[i].ToString()   +   " ' ";
                                SqlDataReader   reader2   =   com.ExecuteReader();
                                string   text8   =   " ";
                                string   text9   =   " ";
                                int   num2   =   0;
                                if   (reader2.Read())
                                {
                                        text8   =   reader2[ "YX_ProName "].ToString();
                                        num2   =   Convert.ToInt16(reader2[0].ToString());
                                }
                                string   Insreader2   =   reader2[0].ToString();
                                reader2.Close();


                                com.CommandText= "select   YX_Spic   from   YX_Product   where   YX_ID= "   +   Convert.ToInt16(Insreader2);
                                SqlDataReader   reader3   =   com.ExecuteReader(CommandBehavior.CloseConnection);
                                if   (reader3.Read())
                                {
                                        text9   =   reader3[0].ToString();
                                }
                             
                                DataRow   row   =   table.NewRow();
                                row[0]   =   num2;
                                row[1]   =   text8;
                                row[2]   =   num;
                                row[3]   =   text9;
                                table.Rows.Add(row);   reader3.Close();
                        }
                       
                        table.DefaultView.Sort   =   "YX_ProNum   desc ";
                        DataRowCollection   rows   =   table.Rows;
                        int   count   =   table.Rows.Count;
                        if   (count   !=   0)
                        {
                                text2   =   rows[0][ "YX_ProPic "].ToString();
                                text3   =   rows[0][ "YX_ID "].ToString();
                                text4   =   rows[0][ "YX_ProName "].ToString();
                                if   (text3   ==   " ")
                                {


                                        text3   =   "0 ";
                                }
                                int   num4   =   1;
                                for   (int   i   =   1;   i   <   count;   i++)
                                {
                                        if   (num4   > =   10)
                                        {
                                                break;
                                        }
                                        string   text11   =   text5;
                                        text5   =   text11   +   " <TR> <TD   bgColor= '#cccccc '   colSpan= '2 '   height=1> </TD> </TR> \n <TR> \n <TD   onmouseover=\ "this.style.backgroundColor= '#EDEBEF '\ "   style=\ "CURSOR: '   hand ';   HEIGHT:   '21px ';   BACKGROUND-COLOR:   '#ffffff '\ "   onmouseout=\ "this.style.backgroundColor= '#FFFFFF '\ "   align=left   colSpan=2   height=21> \n <a   href={$Hope_SitUrl$}ShowProduct.aspx?YX_MID= "   +   rows[i][ "YX_ID "].ToString()   +   "   style= 'color:#676767 '> "   +   newstr(rows[i][ "YX_ProName "].ToString(),   12)   +   " </a> \n </TD> </TR> \n ";
                                        num4++;
                                }
                        }
                        this.Exsql.Close();

  public   class   YX_sql
        {
                public   SqlConnection   con;
                public   YX_sql()
                {
                        this.con   =   new   SqlConnection(System.Configuration.ConfigurationSettings.AppSettings[ "YXPath "]);
                }


}

[解决办法]
CloseConnection
受 .NET Framework 精简版的支持。
在执行该命令时,如果关闭关联的 DataReader 对象,则关联的 Connection 对象也将关闭。

热点排行