Alexandre Henrique
Alexandre Henrique

Reputation: 21

Error 26 on SQL with C#, error locating server / instance specified

I am a little new to C# and SQL Server, and I am trying to connect to a SQL Server database with the following function:

private SqlConnection openDatabase() 
{
    String connStr = "Data Source=|DataDirectory|\\usuariosDB.sdf;Password=senh@1;Persist Security Info=True";
    SqlConnection sqlcon = new SqlConnection(connStr);
    sqlcon.Open();

    return sqlcon;
}

However, when this function is called the Visual Studio debug throws the error 26, that is error locating server / instance specified. What am I doing wrong?

Upvotes: 0

Views: 5563

Answers (3)

Vítor Martins
Vítor Martins

Reputation: 1440

Here is an example of a connection string I'm using:

public const string ConStr = "Data Source=SQLEXPRESS;Integrated Security=True";

Hope it helps ;)

Upvotes: -1

Alexandre Henrique
Alexandre Henrique

Reputation: 21

I've found the answer here:

SQL Server Compact Edition 4.0: Error: 26 - Error Locating Server/Instance Specified

SQL Compact needs SqlCeConnection objects, not SqlConnection !!!

Upvotes: 1

Slider345
Slider345

Reputation: 4758

Take a look at connectionstrings.com for some help on the syntax here. I suspect you need to provide an actual directory path, instead of |DataDirectory|, inside your connection string.

The error you are getting indicates SqlConnection object cannot connect to that server, because it does not exist, which most likely means there is a problem with the path you provided.

Upvotes: 3

Related Questions