
sqlCommand关闭出现问题.
有一个类:Dataase.cs:usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Li...
有一个类:
Dataase.cs:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.Common;
using System.Web;
/// <summary>
/// Summary description for DataBase
/// </summary>
public class DataBase
{
private SqlConnection con=new SqlConnection();
private void Open()
{
if (con==null)
{
con = new SqlConnection("Data Source=58.17.30.81;Initial Catalog=a1230192748;Persist Security Info=True;User ID=a1230192748;Password=44670287");
}
if (con.State == System.Data.ConnectionState.Closed)
{
con.ConnectionString = "Data Source=58.17.30.81;Initial Catalog=a1230192748;Persist Security Info=True;User ID=a1230192748;Password=44670287";
con.Open();
}
}
public void Close()
{
if (con != null && con.State != System.Data.ConnectionState.Open)
con.Close();
}
public DataBase()
{
//
// TODO: Add constructor logic here
//
}
public string liuyan(string id,string sign)
{
string com=string.Empty;
switch(sign)
{
case "xiaobiaoti":
com="Select subject from liuyan where liuyanid='"+id+"'";
break;
case "def_message":
com="Select message from liuyan where liuyanid='"+id+"'";
break;
}
SqlCommand myCommand=new SqlCommand(com,con);
Open();
try
{
SqlDataReader sdr=myCommand.ExecuteReader();
if (sdr.Read())
{
return sdr[0].ToString();
}
else
{
return "";
}
sdr.Close();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write("<script>alert('出错:" + ex.Message + "')</script>");
return "";
}
finally
{
myCommand.Dispose();
Close();
}
}
}
在asps.cs代码中我创建一个DataBase实体,
两次调用 public string liuyan(string id,string sign)
第一次没有异常,第二次有异常,我在ex.message的内容是
There is already an open DataReader associated with this Command which must be closed first.但我在finally中加了myCommand.Dispose();
sdr打开后,再用sdr.Close()关闭了. 展开
Dataase.cs:
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.Common;
using System.Web;
/// <summary>
/// Summary description for DataBase
/// </summary>
public class DataBase
{
private SqlConnection con=new SqlConnection();
private void Open()
{
if (con==null)
{
con = new SqlConnection("Data Source=58.17.30.81;Initial Catalog=a1230192748;Persist Security Info=True;User ID=a1230192748;Password=44670287");
}
if (con.State == System.Data.ConnectionState.Closed)
{
con.ConnectionString = "Data Source=58.17.30.81;Initial Catalog=a1230192748;Persist Security Info=True;User ID=a1230192748;Password=44670287";
con.Open();
}
}
public void Close()
{
if (con != null && con.State != System.Data.ConnectionState.Open)
con.Close();
}
public DataBase()
{
//
// TODO: Add constructor logic here
//
}
public string liuyan(string id,string sign)
{
string com=string.Empty;
switch(sign)
{
case "xiaobiaoti":
com="Select subject from liuyan where liuyanid='"+id+"'";
break;
case "def_message":
com="Select message from liuyan where liuyanid='"+id+"'";
break;
}
SqlCommand myCommand=new SqlCommand(com,con);
Open();
try
{
SqlDataReader sdr=myCommand.ExecuteReader();
if (sdr.Read())
{
return sdr[0].ToString();
}
else
{
return "";
}
sdr.Close();
}
catch (Exception ex)
{
HttpContext.Current.Response.Write("<script>alert('出错:" + ex.Message + "')</script>");
return "";
}
finally
{
myCommand.Dispose();
Close();
}
}
}
在asps.cs代码中我创建一个DataBase实体,
两次调用 public string liuyan(string id,string sign)
第一次没有异常,第二次有异常,我在ex.message的内容是
There is already an open DataReader associated with this Command which must be closed first.但我在finally中加了myCommand.Dispose();
sdr打开后,再用sdr.Close()关闭了. 展开
1个回答
展开全部
一个command只能同时打开一个Reader,你可以使用多个连接和命令去打开REader
return sdr[0].ToString(); 想这样的建议用Command的ExecuteScalar()直接返回一个值,别用Raeder
return sdr[0].ToString(); 想这样的建议用Command的ExecuteScalar()直接返回一个值,别用Raeder
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询