Is anyone aware of any subtle differences between the behavior of SQL Server 2005 when executing a command via OSQL vs. a programmatic query using SQlConnection?
I have a client with a database on a remote machine that I can use OSQL to interact with. However, when I attempt a similar query via building a sqlconnection/sqlcommand; I receive the error:
['A network-related or instance-specific error occurred while establishing a connection to the SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that the SQL Server is configured to allow remote connections (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specific).
Here is the OSQL command which works:
osql -S myHost\SQLEXPRESS -U sa -q "USE [TempDB] select [Endpoint] from [Service]"
This doesn't work, the error above is generated.
SqlConnectionStringBuilder cnBldr = new SqlConnectionStringBuilder();
cnBldr.UserID = "sa";
cnBldr.Password = "PASSWORD";
cnBldr.DataSource = "myHost\\SQLEXPRESS";
cnBldr.InitialCatalog = "TempDB";
using (SqlConnection cn = new SqlConnection(cnBldr.ConnectionString))
using (SqlCommand cmd = new SqlCommand("select [Endpoint] from [Service]",cn))
{
cn.Open();
SqlDataReader rdr = cmd.ExecuteReader();
rdr.Read();
}
In both cases, I am running as the same user (ie: I log in as a windows user and I either open a cmd window and execute the OSQL command or I launch a windows application which contains the code fragment above. The OSQL will work, the code fragment generates the error above. Both cases utilize the 'sa' account for accessing the database. The OSQL fragment will prompt the user to input the password.
Is there some difference between the two that could attribute to this behavior? I don't believe that this is due to a problem with the number of connections as I can freely switch between executing the osql command (which will work) and running the application (which generates the error message).
Thanks for any thoughts.
-john