UPDATE: I have determined that the SQLClient code times out on the first call, but works on all subsequent calls. Also, when connecting with Management Studio, it times out the first time as well, then connects when I try again. Ideas?
Original: Hey guys, I have a very strange issue that I can't seem to resolve. I have an application that connects to several different versions of SQL Server. I have no problems connecting to 2005 or 2008 R2. But I am having timeout issues with 2008 Standard. I even wrote a sample app to try and troubleshoot this. Here's the sample code that works:
OdbcConnection conn = null;
OdbcDataAdapter adp = null;
DataSet resultSet = new DataSet();
DataTable result = null;
string queryString = Query.Text;
conn = new OdbcConnection(@"Driver={SQL Server Native Client 10.0};Server=MyServer\DataSource;Database=MyDatabase;Uid=MyUser;Pwd=ThePassword;");
conn.Open();
adp = new OdbcDataAdapter(queryString, conn);
adp.Fill(resultSet);
result = resultSet.Tables[0];
ResultsGrid.DataSource = result;
Now here the sample code that times out:
SqlConnection conn = null;开发者_如何学编程
SqlDataAdapter adp = null;
DataSet resultSet = new DataSet();
DataTable result = null;
string queryString = Query.Text;
conn = new SqlConnection(Application Name=HCGPrevision; Data Source=MyServer\DataSource; Initial Catalog=MyDatabase;UID=MyUser;PWD=ThePassword;);
conn.Open();
adp = new SqlDataAdapter(queryString, conn);
adp.Fill(resultSet);
result = resultSet.Tables[0]; //Assume only one result set/table.
ResultsGrid.DataSource = result;
This second set of code will work on a database in SQL Server 2008 R2, but not the Standard/R1 version. The connection string seems to work fine for any other SQL Server but won't work for this one. We have been using this code for over 5 years and never had this issue. I made sure that SQL has Named Pipes enabled and Mixed (Windows/SQL) Authentication. The fact that the ODBC will work but not the SQL is bizarre. Please let me know what I'm missing!
Thanks,
Erick
Okay, I finally figured out an answer to this. It tested my code against SQL2005 and started getting the same problem. So I did some more searching and stumbled on this:
http://blogs.msdn.com/b/adonet/archive/2010/04/18/sqlclient-default-protocol-order.aspx
I added "np:" to the Data Source in my connection string and Viola! Connection time!
精彩评论