0

So I've got a little (and valid) .MDF database file (SQL Server). However when I try to access it (line 32 here) or smaller context here...

private void loadDataBaseButton_Click(object sender, EventArgs e)
{
    DialogResult result = openFileDialog1.ShowDialog();

    if (result == DialogResult.OK)
    {
        string databasePath = openFileDialog1.InitialDirectory + openFileDialog1.FileName;
        //SqlConnection dataBaseConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename={0};Integrated Security=True;Connect Timeout=30;User Instance=True");
        SqlConnection dataBaseConnection = new SqlConnection(string.Format(@"Data Source=.\SQLEXPRESS;AttachDbFilename={0};Integrated Security=True;Connect Timeout=30;User Instance=True", databasePath));

        try
        {
            dataBaseConnection.Open();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

However it throws an exception. Specifically it is an

Error 26 - Error Locating Server/Instance Specified

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 .MDF file is valid and everything else to my knowledge seems to check out. How do I fix this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Sql Server database's files have the MDF extension. https://stackoverflow.com/questions/1175882/what-is-an-mdf-file – Steve Jun 29 '18 at 19:14

3 Answers3

0

In your code problem is Connection string.

connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-20180630124430.mdf;Initial Catalog=aspnet-WebApplication1-20180630124430;Integrated Security=True"

this is currect connection string if you using to attached .mdf file. if you connecte to sql server then connection string is

data source=DHARMESH-PC;initial catalog=AdventureWorks;user id=sa;password=sa123

Hope this will help you

Thanks

Update:-

DialogResult result = openFileDialog1.ShowDialog();

        if (result == DialogResult.OK)
        {
            string databasePath = openFileDialog1.InitialDirectory + openFileDialog1.FileName;
            string dbname = openFileDialog1.FileName.Split('.')[0];
            //SqlConnection dataBaseConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename={0};Integrated Security=True;Connect Timeout=30;User Instance=True");
            string connection = @"Data Source=(LocalDb)\v11.0;AttachDbFilename=" + databasePath + ";Initial Catalog=" + dbname + ";Integrated Security=True";
            SqlConnection dataBaseConnection = new SqlConnection(connection);

            try
            {
                dataBaseConnection.Open();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

I have update your code .

You have to select only MDF file .

Dharmeshsharma
  • 683
  • 1
  • 11
  • 28
  • I tried that string with some modification "SqlConnection dataBaseConnection = new SqlConnection(string.Format(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\PopulationDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"));" but it didn't work. :( – serialconnectorzrcool Jun 29 '18 at 19:39
  • @serialconnectorzrcool I have update your code and its working fine. I you need sample let me know – Dharmeshsharma Jun 30 '18 at 05:29
0

I believe if you're trying to connect to "SQLEXPRESS" that would imply that you are running SQL Server Express locally and attaching the database

I've been able to successfully access localdb MDF files with a connection string like this:

connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDBFile.mdf;Integrated Security=True" providerName="System.Data.SqlClient"

You should also verify that the file is in the expected "DataDirectory."

Rick Riensche
  • 1,050
  • 1
  • 12
  • 25
  • Well for the sake of neater-ish code I tried doing this " SqlConnection dataBaseConnection = new SqlConnection(@"Data Source=LocalDB)\PopulationDB;AttatchedDbFileName=|DataDirectory|\PopulationDB.mdf;Integrated Security = True");" but that threw an exception saying that "AttatchedDBFileName" is an unknown keyword. Now what? Ugh I hate SQL :(. – serialconnectorzrcool Jun 29 '18 at 20:57
  • If tha'ts a direct copy-paste, looks like missing an open paren before "LocalDB" and make sure it's "AttachDbFilename" not "AttachedDbFilename" :) Also, I am not 100% sure but I think that "MSSQLLocalDB" might be a hard-coded default (i.e., unrelated to the name of your actual database) – Rick Riensche Jun 29 '18 at 21:06
  • Well progress I guess? Anyway I fixed what you said and I changed the Data Source=(LocalDB)\PopulationDB to Data Source=(LocalDB)\MSSQLLocalDB". Now no exception is thrown but the dataBaseConnection (the sqlConnection) is null. So now what? – serialconnectorzrcool Jun 29 '18 at 21:30
0

If you are using

User Instance=true

you must use named pipes so your connection will change from:

.\SQLEXPRESS

to

.\\SQLEXPRESS

The network protocol for user instances must be local Named Pipes. A user instance cannot be started on a remote instance of SQL Server, and SQL Server logins are not allowed.

sql-server-express-user-instances

vfrank66
  • 1,318
  • 19
  • 28