简体   繁体   中英

Having trouble connecting to a SQL Server database file. What am I doing wrong?

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?

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 .

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM