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?

Sonhja
  • 8,230
  • 20
  • 73
  • 131

3 Answers3

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"
Community
  • 1
  • 1
Radim Köhler
  • 122,561
  • 47
  • 239
  • 335
  • 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 ] – Sonhja Jan 11 '13 at 10:14
  • `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 – Radim Köhler Jan 11 '13 at 10:15
  • 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 – Radim Köhler Jan 11 '13 at 10:16
  • 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? – Sonhja Jan 11 '13 at 10:20
  • no! .\InstanceName means localhost\InstanceName. So change it to the name of your server (your workstation) – Radim Köhler Jan 11 '13 at 10:23
  • Mine is SQLEXPRESS too... I don't know what's wrong with my connection string right now... – Sonhja Jan 11 '13 at 10:24
  • please, check this: http://www.connectionstrings.com/sql-server-2008. typical is `Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=dbname; Trusted_Connection=Yes; ` so instead of .sdf (used mostly for CE) the extension should be **.mdf** – Radim Köhler Jan 11 '13 at 10:29
  • Ok, solved! At least the .Open(). You were right. It was the .sdf file, but as I couldn't create it for regular sql expressions, I had to change to SqlCeConnect, and now, FINALLY... works!!! :))) Sooooooooooooo thanks!!!! – Sonhja Jan 11 '13 at 10:51
0

It just means that the server was not found.

  • Check the connection string
  • the server is not running at all
  • check for firewall
John Woo
  • 258,903
  • 69
  • 498
  • 492
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();
Sonhja
  • 8,230
  • 20
  • 73
  • 131