ASP.NET中SqlConnection、SqlDataReader对象打开关闭问题
SqlConnection conData;
SqlCommand cmd;
SqlDataReader cityName;
conData=new SqlConnection("workstation id=house;packet size=4096;user id=sa;initial catalog=samples;persist security info=True;password=1234");
cmd=new SqlCommand("Select * from citys",conData);
conData.Open();
cityName=cmd.ExecuteReader();
city.DataSource=cityName;
city.DataBind();
cityName.Close();
conData.Close();
在执行的时候总是提示conData已经打开,如果我把conData.Open();改为:
if(conData.State==ConnectionState.Closed)
{
conData.Open();
}
又提示:已有打开的与此连接相关联的 DataReader,必须首先将它关闭
请问高手这是为什么啊,我开机后并未运行其他的aspx文件啊?
另外跟这个代码一样的aspx文件跟这个文件在一个项目中的却可以正常运行如下:
SqlConnection conData;
SqlCommand cmd;
SqlDataReader UserInfo;
conData=new SqlConnection("workstation id=house;packet size=4096;user id=sa;initial catalog=samples;persist security info=True;password=1234");
cmd=new SqlCommand("Select * From Users",conData);
conData.Open();
UserInfo=cmd.ExecuteReader();
Users.DataSource=UserInfo;
Users.DataBind();
UserInfo.Close();
conData.Close();
这是为什么啊?