2

On Visual Studio 2010, I have a database that I'm trying to connect to, but when I try to do:

db.Open();

It launches this mistake:

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 tried to do what this link says, but I still keep having the same mistake.

Any ideas on what's going on?

EDIT: Firewall is OFF.

Connection String: MySQLProject.Properties.Settings.Default.dbConnectionString = "Data Source=|DataDirectory|\db.sdf"

Server is up:

enter image description here

EDIT2:

This is the code that fails:

public void FillData()
    {
        // 1 step. Open connection
        // SqlConnection c = new SqlConnection("Data Source=" +db.Connection.DataSource);
        SqlConnection c = new SqlConnection(MySQLProject.Properties.Settings.Default.dbConnectionString);
        try
        {
            c.Open();

            // 2 step. Create new DataAdapter
            using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM USER", c))
            {
                // 3 step. Use DataAdapter to fill table
                DataTable t = new DataTable();
                a.Fill(t);
                // 4 step. Render data on the DataGridView
                dataGridViewUsers.DataSource = t;
            }
        }
        catch (SqlException e)
        {
            MessageBox.Show(e.Message);
        }
    }

EDIT nº 1000:

Ok, I used this connection string:

string con2 = @"Server=.\SQLExpress;AttachDbFilename=|DataDirectory|db.sdf;Database=db;Trusted_Connection=Yes;";

And then it says this:

enter image description here

:____(

Ok, now i Know that .sdf is for CE Sql statements. But, I can't create .mdf, don't know exactly why... Should I change to CE Sql statements?

4
  • Verify your conectionstring.... Commented Jan 11, 2013 at 10:02
  • not valid attch db name, means, that db.dsf should be rpelaced e.g. with aspnet.mdf. you are almost there ;) Commented Jan 11, 2013 at 10:33
  • But my db is .sdf, not .mdf... So should I create a new one? .sdf are for SqlCeStatements, isn't that? Yea, almost there :P PS: Can't create .mdf DB!!! Commented Jan 11, 2013 at 10:35
  • yes, .mdf is for express... .sdf for CE (compact edition). In fact the suffix should not play the role... but it is a standard Commented Jan 11, 2013 at 10:37

3 Answers 3

1

The most typical reasons are:

  • The server is not runing
  • Connection string has typo (some letters are wrong)

So when connecting to not existing or shuted down server, this message appears

EDIT: please try to check this: SQL Server Express connection string for Entity Framework Code First, because it seems, that your conn string is not complete:

connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
                       AttachDBFilename=|DataDirectory|aspnetdb.sdf;
                       User Instance=true"
Sign up to request clarification or add additional context in comments.

8 Comments

If I follow that thread, let's say it's because of that, that our connection string doesn't have the correct format to use a SqlConnection (it looks like that), and I change it to a SqlCeConnection, it says this mistake: There was an error parsing the query. [ Token line number = 1, Token line offset = 15, Token in error = USER ]
SqlCeConnection is for a SQL Compact edition, it is not your case, as I see now. But it most likely would be incomplete connection string
Again, this message, always means, that the ADO.NET has incorrect connection string. So, while server is running, we must adjust connection string. This is the place where is the issue at the moment
If I use this string: connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|aspnetdb.sdf; User Instance=true" the .\ launches a mistake, so I changed it to //. Is that ok?
no! .\InstanceName means localhost\InstanceName. So change it to the name of your server (your workstation)
|
0

It just means that the server was not found.

  • Check the connection string
  • the server is not running at all
  • check for firewall

Comments

0

Only for those who find the same problem:

As I couldn't create a .mdf database, only .sdf one, as @Radim Köhler says, .sdf databases I had to use SqlCe... instances. My connection string says:

@"Data Source=|DataDirectory|db.sdf;Persist Security Info=False;"

And I connected like this:

SqlCeConnection c = new SqlCeConnection(con3); //MySQLProject.Properties.Settings.Default.dbConnectionString);
            c.Open();

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.