简体   繁体   中英

SQL Server Connection Timeout C#

First off I'd like to let everyone know I have searched my particular problem and can't seem to find what's causing my problem.

I have an SQL Server 2008 instance running on a network machine and a client I have written connecting to it.

To connect I have a small segment of code that establishes a connection to an sql server 2008 instance and returns a DataTable populated with the results of whatever query I run against the server, all pretty standard stuff really. Anyway the issue is, whenever I open my program and call this method, upon the first call to my method, regardless as to what I've set my Connection Timeout value as in the connection string, it takes about 15 seconds and then times out. Bizarrely though the second or third call I make to the method will work without a problem.

I have opened up the ports for SQL Server on the server machine as outlined in this article: How to Open firewall ports for SQL Server and verified that it is correctly configured. Can anyone see a particular problem in my code?

    string _connectionString = "Server=" + @Properties.Settings.Default.sqlServer + "; Initial Catalog=" + @Properties.Settings.Default.sqlInitialCatalog +
        ";User Id=" + @Properties.Settings.Default.sqlUsername + ";Password=" + @Properties.Settings.Default.sqlPassword + "; Connection Timeout=1";

    private DataTable ExecuteSqlStatement(string command)
    {
        using (SqlConnection conn = new SqlConnection(_connectionString))
        {
            try
            {
                conn.Open();
                using (SqlDataAdapter adaptor = new SqlDataAdapter(command, conn))
                {
                    DataTable table = new DataTable();
                    adaptor.Fill(table);
                    return table;
                }
            }
            catch (SqlException e)
            {
                throw e;
            }
        }
    }

The SqlException that is caught at my catch is : "Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding."

This occurs at the conn.Open(); line in the code snippet I have included. If anyone has any ideas that'd be great!

Addendum If I ping the server it's <10ms response time. Additionally I'm getting the same behavior when using SQL Server Management studio (I've just tried this after writing this question as the thought just crossed my mind). I would presume that this has to be a network issue as opposed to a code issue as it also takes two attempts when using SQL Server Management studio. I realise I can implement an error check for connection failure and then re-attempt the connection (I should probably do this anyway) but this doesn't seem particularly 'clean' to me as it doesn't address the underlying problem. Does anyone here have any experience with this issue?

Edit Although over a year later this question has been viewed over 4000 times, I've since discovered that although I /thought/ the firewall ports were opened, they actually weren't. I'd incorrectly assumed that you need to open the firewall in Windows to the port number that SQL Server uses, in my case I set it to allow connections on TCP port 1433, the firewall caused issues when attempting to establish an initial connection.

I discovered that the correct way to set it up was to set Windows Firewall to allow the SQL Server Instance executable through. To do this you need Add a Program in Windows Firewall, browse to the folder your sql instance resides in such as C:\\Program Files (x86)\\Microsoft SQL Server\\MSSQL10_50.SQLINSTANCE\\MSSQL\\Binn and then add the sqlservr.exe file to the firewall rules.

You are setting the connection timeout value to 1 second. Drop it from connection string and try again.

Or the problem could be with the SQL select you are using. Could it be taking a long time. If so, the 2nd and 3rd calls would retrieve the cached values and thus run much quicker.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM