将DataReader从Method传递到.cs页 - 使用CommandBehavior.CloseConnection但连接未关闭 [英] Passing DataReader from Method to .cs page - use CommandBehavior.CloseConnection but connection not closing
问题描述
我在类中使用以下方法 - 我从.cs页面填充datareader调用:
I use the following method in a class - which I call from .cs page filling a datareader:
public static SqlDataReader getinfo(string username, string url)
{
//Initialise connection, string stored in Web.config
SqlConnection conNs = new SqlConnection(ConfigurationManager.ConnectionStrings["conNs"].ConnectionString);
SqlCommand cmdNs = new SqlCommand("usp_getinfo", conNs);
cmdNs.CommandType = CommandType.StoredProcedure;
cmdNs.Parameters.Add(new SqlParameter("@username", SqlDbType.VarChar, 50));
cmdNs.Parameters["@username"].Value = username;
cmdNs.Parameters.Add(new SqlParameter("@url", SqlDbType.VarChar, 50));
cmdNs.Parameters["@url"].Value = url;
//Execute Command
conNs.Open();
SqlDataReader dr = cmdNs.ExecuteReader(CommandBehavior.CloseConnection);
//Return data
return dr;
}
注意'commendbehavior.closeconnection'。
Notice 'commendbehavior.closeconnection'.
然后在.cs页面中我这样调用:
Then in a .cs page I call it like this:
SqlDataReader drInfo = dataAccess.getinfo(username, url);
//do some stuff
drInfo.Close();
这应该关闭连接吗?
我有一个问题,应用池填满了打开的连接。我的技术有问题吗?
I am having a problem with the apppool filling up with open connections. Is there a problem with my technique?
速度是我的首要任务。
感谢
推荐答案
它看起来不像GetInfo返回很多数据。你最好不要像这样传递DataReader。在GetInfo中,执行你的Read并返回一个包含信息的对象,而不是返回DataReader。速度的差异将是无关紧要的,你将消除你经历的所有痛苦。
it doesn't look like GetInfo returns a lot of data. You're better off not passing a DataReader around like this. Inside GetInfo, perform your Read and return an object that contains the information instead of returning the DataReader. The difference in speed will be inconsequential, and you will eliminate all the pain you're going through.
这篇关于将DataReader从Method传递到.cs页 - 使用CommandBehavior.CloseConnection但连接未关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!