Cannot open SqlConnection: "A network-related or instance-specific error occurred while establishing

Trips

Well-known member
Joined
Aug 7, 2010
Messages
2,788
Im new to C#, and have been having problems trying to connect to a local database using an SqlConnection in a simple app Im trying to create. I started in Visual C# 2010 Express by selecting Data > Add new Data Source, and followed the steps in the
wizard, selecting a database with a dataset type model. I then added the tables and columns I wanted to add in the empty database in the database explorer.
I have created an SQLConnection in code, passing the connection string to the constructor. However, when I try to open this connection, it throws an exception with the message:
"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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
The connection string I am using is: "Data Source=C:\Users\... \Visual Studio 2010\Projects\MusicDatabase\MusicDatabase\MusicDB.sdf;Persist Security Info=True". The path for the connection string is the same as the connection string in the
database properties tab.
I checked the connection properties in database explorer and made sure that there was a saved password which matched the one in the database, and made sure that the Test Connection button succeeded without error. I tried adding "Password=xxx" (where
xxx is the password of the database) to the start of the connection string to make sure that this wasnt, but this did not fix the error.
I have since removed the password from the database altogether in the database properties, and this doesnt fix the problem either, so I dont think it is anything to do with the password.
I dont have any experience with creating database connections, so Im not sure what Ive done wrong, or if there are any steps Ive missed, or if theres any additional information that might be helpful. Any help appreciated.

View the full article
 
Back
Top