0

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

1

5 Answers 5

1

maybe your connectionString should like this?

connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=databaseName;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\my.mdf"
Sign up to request clarification or add additional context in comments.

Comments

1

Try to specify a full path to the database:

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

See more here

Comments

0

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";

2 Comments

so where is m database file name?
in connection string as far as I know,we have to use dbserver name and not .mdf file name or path
0

Try this connection string.

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

Comments

0

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;");   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.