0
votes

I'm going to create a SQL Server database file (.MDF file) in Visual Studio 2012 (C#).

I'm working on a desktop application, I've added a new .MDF file to project, but I don't know what is my connection string, I get this error when I try to connect to my DB:

SqlConnection SQLConnection = new SqlConnection("Data Source=db\\ofoghdb.mdf");
SQLConnection.Open();

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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

I'm fairly familiar with SQL Server in web development, but I'm going to use it in a desktop (winform) app and I get above error

5
There are a few reason for this error. sswug.org/articlesection/default.aspx?TargetID=44331 - Soner Gönül

5 Answers

1
votes

Try to specify a full path to the database:

SqlConnection SQLConnection = new SqlConnection(@"Data Source=C:\ofoghdb.mdf");
SQLConnection.Open();

See more here

1
votes

maybe your connectionString should like this?

connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=databaseName;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\my.mdf"
0
votes

You should be using like this to connect to a database server:

SqlConnection oSQLConn = new SqlConnection();
oSQLConn.ConnectionString = "Data Source=(local);" +
                            "Initial Catalog=mySQLServerDBName;" +
                            "Integrated Security=SSPI";
0
votes

Try this connection string.

SqlConnection SQLConnection = new SqlConnection(@"Server=(localdb)\v11.0;Integrated Security=true;AttachDbFileName=DB\MyData.mdf");
SQLConnection.Open();
0
votes

Try this

OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" 
    + Environment.GetFolderPath(Environment.SpecialFolder.Personal) 
    + "\\folder name\\Databass name " + ";Persist Security Info=False;");