sql和池连接错误

时间:2010-04-17 05:56:22

标签: asp.net sql connection-pooling

请查看以下代码,因为此示例代码在Hostexcellence上托管时出现错误,但本地运行完美,错误如下:

  

超时已过期。从池中获取连接之前经过的超时时间。这可能是因为所有池化连接都在使用中并且达到了最大池大小

SqlDataSource1.SelectCommand = "Select Top (3) * from News Order by NewsID Desc";
SqlDataSource1.DataSourceMode = SqlDataSourceMode.DataReader;
SqlDataReader r_News = (SqlDataReader)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
DataGrid_News.DataSource = r_News;
r_News.Close();
DataGrid_News.DataBind();

那么代码有什么问题?

1 个答案:

答案 0 :(得分:1)

请参阅:http://msdn.microsoft.com/en-us/library/s4yys16a(VS.71).aspx

Public Sub ConnectToSql()
    Dim conn As New SqlClient.SqlConnection
    ' TODO: Modify the connection string and include any
    ' additional required properties for your database.
    conn.ConnectionString = & _
    "integrated security=SSPI;data source=SQL Server Name;" & _
    "persist security info=False;initial catalog=northwind"
    Try
        conn.Open()
        ' Insert code to process data.
    Catch ex As Exception
        MessageBox.Show("Failed to connect to data source")
    Finally
        conn.Close()
    End Try

End Sub

您应该始终包含finally子句以确保您的连接已关闭,否则将不会释放连接(如果发生异常)并且您将不再有可用的连接。