2010-04-17 116 views
0

请看看下面的代码此示例代码给出了一个错误,当我主持它Hostexcellence,但在本地运行完美,误差为以下几点:SQL和池连接错误

超时过期。在从池中获取连接之前已超时。出现这种情况可能是因为所有池连接使用,最大池大小达到

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

看到这个: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 

你应该始终包括最后条款,以确保您的连接被关闭,否则连接不会被释放(如果发生异常时),你不会有任何更多可用的连接。