如果要返回DataReader的话,在自定义类中打开的Connection应该使用
SqlDataReader sdr = SqlCommandInstance.ExecuteReader(CommandBehavior.CloseConnection);
这样调用者只需要关闭sdr即可,对应的Connection会自动关闭
SqlConnection可设定connection pool的maxpoolsize和minpoolsize
例如:str_Conn="data source=ntserver;initial catalog=gc_web;persist security info=true;"+
"Enlist=true;Pooling=true;Max Pool Size=300;Min Pool Size=0;Connection Lifetime=300;user id=sa;packet size=4096";
还可以设定SqlCommand.CommandTimeOut 或者Sqlconnection.GeneralTimeOut
这个方法应该是没什么问题吧
Database data = new Database();
string strConnection = data.DbPath();
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
SqlCommand objCommand = new SqlCommand(sql,objConnection);
SqlDataReader myReader = objCommand.ExecuteReader();
this.Repeater5RcPhoto.DataSource = myReader;
this.Repeater5RcPhoto.DataBind();
myReader.Close();
objCommand.Dispose();
objConnection.Close();
看看微软的写法:
Public Sub ReadMyData(myConnString As String)
Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders"
Dim myConnection As New SqlConnection(myConnString)
Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
myConnection.Open()
Dim myReader As SqlDataReader
myReader = myCommand.ExecuteReader()
While myReader.Read()
Console.WriteLine(myReader.GetInt32(0) & ", " & myReader.GetString(1))
End While
' always call Close when done reading.
myReader.Close()
' Close the connection when done with it.
myConnection.Close()
End Sub 'ReadMyData
解决方法(*):
WEB.config 里面:
在数据库连接加 Max Pool Size = 512;
server=local;uid=;pwd=;database=2004;Max Pool Size = 512;">
一劳永逸。
转载于:https://www.cnblogs.com/cooltonyhawk/articles/1229347.html