一个很着急的数据库连接问题,能解决问题就高分送
我用asp.net写了个应用,在连接过程中,因为采用了大量的存储过程,每次在调用存储过程的时候就会有一个连接到SQLServer,当打开某些页面时,在SQLServer中查进程,竟然会同时出现100多个连接。请问用什么方法可以解决。
我试了用Application和Session/Cache都不行,因为在存储过程中还有大量的Dataset Reader冲突。现在非常着急,请大家帮帮忙,帮我解决这个问题,谢谢。
下面是程序段,这样的数据库连接在程序中有很多次:
public DocClassCollection GetAllClassesByGroupID(int userid,int groupid,int startrecord)
{
// return all of the docclasss and their total and daily posts
// Create Instance of Connection and Command Object
SqlConnection myConnection = new SqlConnection(Globals.DatabaseConnectionString);
SqlCommand myCommand = new SqlCommand("up_GetAllClassesByGroupID", myConnection);
// Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure;
// Add Parameters to SPROC
SqlParameter para1 = new SqlParameter("@userid", SqlDbType.Int);
para1.Value = userid;
myCommand.Parameters.Add(para1);
// Add Parameters to SPROC
SqlParameter para2 = new SqlParameter("@groupid", SqlDbType.Int);
para2.Value = groupid;
myCommand.Parameters.Add(para2);
// Add Parameters to SPROC
SqlParameter para3 = new SqlParameter("@startrecord", SqlDbType.Int);
para3.Value = startrecord;
myCommand.Parameters.Add(para3);
// Add Parameters to SPROC
SqlParameter para4 = new SqlParameter("@pagesize", SqlDbType.Int);
para4.Value = Globals.DefaultPageSize;
myCommand.Parameters.Add(para4);
// Execute the command
myConnection.Open();
SqlDataReader dr = myCommand.ExecuteReader();
// populate a docDocClassCollection object
DocClassCollection docclasses = new DocClassCollection();
DocClass docclass;
while (dr.Read())
{
docclass = PopulateClassFromSqlDataReader(dr);
docclasses.Add(docclass);
}
dr.Close();
myConnection.Close();
myConnection.Dispose();
return docclasses;
}