Hi,
I've got an C# .Net 3.5 executable that runs on a local machine and makes database calls to a server running SQL Server 2005. I've added a System DSN using the Data Sources manager on the local machine called "localserver" (with the driver set to SQL Server). When I create the DSN, I can test the connection to the server successfully.
I've added localserver to my connection string in my executable, and I get an error "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
Here is my connection string
SqlConnection mainConnection = new SqlConnection(@"Data Source=localserver;database=Closing_Balance;User id=foo;Password=bar;");
The server I am connecting to does allow for remote connections. When I replace localserver with the server name, it connects fine, but this program will be at multiple locations with multiple server names, so I need to make the data source dynamic.
Any ideas as to why this is happening?
Thank you,
Aaron
EDIT:
I've also tried using "localserver,1433" as my data source, but I get this error: "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 0 - No such host is known.)"
EDIT:
Thank you for your responses. To solve my problem, I made a small method to gather the servername using an odbc connection.
private string getServerName()
{
OdbcConnection conn = new OdbcConnection(@"DSN=localserver;Uid=foo;pwd=bar");
string cmdText = "select @@servername";
OdbcCommand cmd = new OdbcCommand(cmdText, conn);
conn.Open();
string server = cmd.ExecuteScalar().ToString();
conn.Close();
conn.Dispose();
return server;
}
I can then send the servername to the SqlConnection. Thanks again for your input.