我的构思是这样的:表示层绑定控件,逻辑层调用数据层执行存储过程并返回数据,然后想用完数据后关掉,结果虽然没有错误 但是提示SqlHelper1.closeConn(); <——检测到无法访问的代码
我想是不能关掉数据库连接,请哪位高手给出这种架构的最好方案,可以用完数据马上关掉
谢谢先!
我的表示层:
HotProduct1.DataSource=Product.HotProducts();//Bussiness.Product
HotProduct1.DataBind();
逻辑层:
public static SqlDataReader HotProducts()
{
string ProcedureName="PR_HotProducts";存储过程
SqlHelper SqlHelper1=new SqlHelper();
SqlHelper1.OpenConn();
return SqlHelper1.ExecDataReader(ProcedureName);
SqlHelper1.closeConn(); <——检测到无法访问的代码
}
数据层:
public SqlDataReader ExecDataReader(string ProcedureName)
{
SqlCommand myCommand=new SqlCommand(ProcedureName,Conn);
myCommand.CommandType=CommandType.StoredProcedure;
SqlDataReader result = myCommand.ExecuteReader();
return result;
}
我想是不能关掉数据库连接,请哪位高手给出这种架构的最好方案,可以用完数据马上关掉
谢谢先!
我的表示层:
HotProduct1.DataSource=Product.HotProducts();//Bussiness.Product
HotProduct1.DataBind();
逻辑层:
public static SqlDataReader HotProducts()
{
string ProcedureName="PR_HotProducts";存储过程
SqlHelper SqlHelper1=new SqlHelper();
SqlHelper1.OpenConn();
return SqlHelper1.ExecDataReader(ProcedureName);
SqlHelper1.closeConn(); <——检测到无法访问的代码
}
数据层:
public SqlDataReader ExecDataReader(string ProcedureName)
{
SqlCommand myCommand=new SqlCommand(ProcedureName,Conn);
myCommand.CommandType=CommandType.StoredProcedure;
SqlDataReader result = myCommand.ExecuteReader();
return result;
}
这一句,也就是return 之后的语句不会再执行了,你这样做
public static SqlDataReader HotProducts()
{
string ProcedureName="PR_HotProducts";存储过程
SqlHelper SqlHelper1=new SqlHelper();
SqlHelper1.OpenConn();
SqlDataReader rtnReader = SqlHelper1.ExecDataReader(ProcedureName);
SqlHelper1.closeConn();
return rtnReader;
}
就可以了
逻辑层:
public static SqlDataReader HotProducts()
{
string ProcedureName="PR_HotProducts";存储过程
SqlHelper SqlHelper1=new SqlHelper();
SqlHelper1.OpenConn();
return SqlHelper1.ExecDataReader(ProcedureName);
//SqlHelper1.closeConn(); <——检测到无法访问的代码
}
数据层:
public SqlDataReader ExecDataReader(string ProcedureName)
{
SqlCommand myCommand=new SqlCommand(ProcedureName,Conn);
myCommand.CommandType=CommandType.StoredProcedure;
SqlDataReader result = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
return result;
}
2。就这个具体的方法来说,如果要返回DataReader类型对象,那就不能调用close之类的方法,否则就不会返回结果。只能在使用HotProducts()的地方调用Close()。
try{你的代码}
catch(){}
finnally{ SqlHelper1.closeConn();}一般有数据库操作都要用到异常处理的吧
这样的话用完之后不能关闭连接啊 微软的示例就是这样的
我这样行不行?
表示层:
HotProduct1.DataSource=Product.HotProducts();//Bussiness.Product
HotProduct1.DataBind();
Product.HotProducts().Close();//关闭reader
return dr;
private void GIA31P1_CloseConnection()
{
string strLog;
if (this.conn != null)
{
try
{
this.conn.Close();
this.conn = null;
}
catch (SqlException se)
{
strLog = se.Message;
}
finally
{
strLog = " finally";
} }
}
public SqlDataReader ExecDataReader(string ProcedureName)
{
SqlCommand myCommand=new SqlCommand(ProcedureName,Conn);
myCommand.CommandType=CommandType.StoredProcedure;
return myCommand.ExecuteReader(CommandBehavior.CloseConnection);
}表示层:
IDataReader reader = Product.HotProducts();
HotProduct1.DataSource=reader;
HotProduct1.DataBind();
reader.Close();或者,(因为 DbEnumerator 自动调用Close(),参考我的blog
http://blog.joycode.com/saucer/archive/2004/07/24/28365.aspx
)HotProduct1.DataSource=Product.HotProducts();
HotProduct1.DataBind();
但也应该考虑一下,把DataReader传到表现层是否恰当,是否应该用业务实体集合