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 

你應該始終包括最後條款,以確保您的連接被關閉,否則連接不會被釋放(如果發生異常時),你不會有任何更多可用的連接。