| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1236 人关注过本帖
标题:[求助]ExecuteReader 需要打开的并且可用的连接。该连接的当前状态是 Close ...
只看楼主 加入收藏
labid
Rank: 1
等 级:新手上路
帖 子:151
专家分:0
注 册:2006-6-20
收藏
 问题点数:0 回复次数:2 
[求助]ExecuteReader 需要打开的并且可用的连接。该连接的当前状态是 Closed。

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

namespace cqjt
{
/// <summary>
/// cqjt 的摘要说明。
/// </summary>
public class cqjt : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataList GgList;
protected System.Web.UI.WebControls.DataList zxdtList;
protected System.Web.UI.WebControls.DataList zxwjList;
protected System.Web.UI.WebControls.Label myLabel;
public string Ip_address;
public string dxdate;
public string zong;
private static String ConnectionString=ConfigurationSettings.AppSettings["ConnStr"];
private void Page_Load(object sender, System.EventArgs e)
{


Ip_address = Request.ServerVariables["REMOTE_ADDR"];
dxdate=System.DateTime.Today.ToString("yyyy-MM-dd");


if(!Page.IsPostBack)// 在此处放置用户代码以初始化页面
{
gettongjicunchu();
getGgList();//公告栏
getzxdtList();//最新动态
getwjList();
gettongji();//最新动态

}
}
private void getGgList() //取得公告数据
{
SqlConnection myConnection = new SqlConnection(ConnectionString);//取连接字符串,建立连接
SqlDataAdapter SqlDataAdapter1 = new SqlDataAdapter();

SqlDataAdapter1.SelectCommand=new SqlCommand("SELECT top 5 newid,title,convert(char(10),dateandtime,120) dateandtime FROM jtnews where dtionid=1 ORDER BY dateandtime DESC ",myConnection);

DataSet DataSet1=new DataSet();
try
{
SqlDataAdapter1.Fill(DataSet1,"jtnews");
}
catch (SqlException e1)
{
Response.Write(e1);
}
GgList.DataSource=DataSet1;
GgList.DataBind();


}

private void gettongjicunchu()
{
SqlConnection myConnection =new SqlConnection(ConnectionString);

String sql="select * from tongji where ip='"+Ip_address+"'and dxdate='"+dxdate+"'" ;
SqlCommand myComm=new SqlCommand(sql,myConnection);


SqlDataReader rs=myComm.ExecuteReader();

if (!rs.Read())
{


SqlCommand SqlComm2=new SqlCommand("addtongji",myConnection);
SqlComm2.CommandType=CommandType.StoredProcedure;

SqlComm2.Parameters.Add(new SqlParameter("@ip",SqlDbType.NVarChar,50)); //调用并设置存储过程参数
SqlComm2.Parameters["@ip"].Value = Ip_address;
//获得新闻内容
SqlComm2.Parameters.Add(new SqlParameter("@dxdate",SqlDbType.NVarChar,50));
SqlComm2.Parameters["@dxdate"].Value =dxdate;

}

}
private void gettongji()
{

SqlConnection myConnection =new SqlConnection(ConnectionString);

String sql="select count(*) as zong from tongji" ;
SqlCommand myComm=new SqlCommand(sql,myConnection);


SqlDataReader rs=myComm.ExecuteReader();
while(rs.Read())
{
zong= rs["zong"].ToString();
}
//SqlDataReader rs=myComm.ExecuteReader();//后还要让游标下移动










}

private void getzxdtList() //取得最新动态数据
{
SqlConnection myConnection = new SqlConnection(ConnectionString);//取连接字符串,建立连接
SqlDataAdapter SqlDataAdapter1 = new SqlDataAdapter();

SqlDataAdapter1.SelectCommand=new SqlCommand("SELECT top 5 newid,title,convert(char(10),dateandtime,120) dateandtime FROM jtnews where developmentid=1 ORDER BY dateandtime DESC ",myConnection);

DataSet DataSet1=new DataSet();
try
{
SqlDataAdapter1.Fill(DataSet1,"jtnews");
}
catch (SqlException e1)
{
Response.Write(e1);
}
zxdtList.DataSource=DataSet1;
zxdtList.DataBind();


}
private void getwjList() //取得最新动态数据
{
SqlConnection myConnection = new SqlConnection(ConnectionString);//取连接字符串,建立连接
SqlDataAdapter SqlDataAdapter1 = new SqlDataAdapter();

SqlDataAdapter1.SelectCommand=new SqlCommand("SELECT top 5 newid,title,convert(char(10),dateandtime,120) dateandtime FROM jtnews where classname='政府文件' ORDER BY dateandtime DESC ",myConnection);

DataSet DataSet1=new DataSet();
try
{
SqlDataAdapter1.Fill(DataSet1,"jtnews");
}
catch (SqlException e1)
{
Response.Write(e1);
}
zxwjList.DataSource=DataSet1;
zxwjList.DataBind();


}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}



while(rs.Read())
为什么我第一运行正常,第二次运行就出错了
高手指点一下好吗?就说这行已经关闭
如标题一样的错误




[此贴子已经被作者于2006-10-18 17:18:01编辑过]

搜索更多相关主题的帖子: using System Closed Web 状态 
2006-10-18 17:13
mylover624
Rank: 1
来 自:乖乖的心中
等 级:新手上路
帖 子:868
专家分:0
注 册:2006-7-6
收藏
得分:0 

在它的前面加上:myConnection.Open();就行了.

还要记住,用完后关闭.


一个天才顶不上十个笨蛋!
书山有路勤为径,学海无涯友相伴。
我的E-mail:mylover624@.cn
2006-10-18 18:33
labid
Rank: 1
等 级:新手上路
帖 子:151
专家分:0
注 册:2006-6-20
收藏
得分:0 
谢谢,`````````高手``````````
2006-10-19 17:22
快速回复:[求助]ExecuteReader 需要打开的并且可用的连接。该连接的当前状态是 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019409 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved