Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two lines of very basic code written in visual studio vb.net to connect to a SQL server that works fine in one instance of visual studio, but does not work when copied over to another instance. I have checked that all the necessary references are included in both instances. Further complicating the issue is that I can connect to a different SQL server in both instances, but I can connect to the server I want in only one instance. Does anyone have a thought as to what may be different between the instances of Visual Studio that would cause this behavior?

The error I am getting is that the server cannot be found. The connection string is identical in all cases

 Dim con As New SqlConnection("Data Source=server1;Initial Catalog=somedatabase;User ID=myid;Password=mypassword;Encrypt=False;TrustServerCertificate=False")
con.Open()


I appreciate any ideas. This is throwing me for a loop.

What I have tried:

I have tried copying the same code to other existing programs and it works just fine. When copied to a new program, it fails.
Posted
Updated 26-Apr-17 10:02am
Comments
ZurdoDev 26-Apr-17 11:32am    
You should put the exact error, but if it is that the server cannot be found, that is the error. Perhaps a firewall is blocking, perhaps that server has a sql instance and not a default. Perhaps you need to use IP of server. There's no way for us to know.

This is a perfect example of why you don't hardcode connection strings!
You need to provide a connection string with a full reference to teh instenace of SQL server, which normally will be something like "COMPUTERNAME\SQLINSTANCE NAME".
For example, one of mine is:
DataSource=GRIFF-DESKTOP\SQLExpress;Initial Catalog=...
You can only normally get away with just the instance name on local DB's

You also need to check that the SQL Server instance is configured to allow remote connections as this is often disabled for security reasons.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900