简体   繁体   中英

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?

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.

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 !!!

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

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

Hope it helps ;)

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