简体   繁体   English

使用C#的SQL上的错误26,指定了错误定位服务器/实例

[英]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: 我对C#和SQL Server有点陌生,并且尝试使用以下功能连接到SQL Server数据库:

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. 但是,调用此函数时,Visual Studio调试会引发错误26,即错误定位指定的服务器/实例。 What am I doing wrong? 我究竟做错了什么?

Take a look at connectionstrings.com for some help on the syntax here. 看看connectionstrings.com ,这里的语法有帮助。 I suspect you need to provide an actual directory path, instead of |DataDirectory| 我怀疑您需要提供实际的目录路径,而不是|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. 您收到的错误表明SqlConnection对象无法连接到该服务器,因为它不存在,这很可能意味着您提供的路径存在问题。

I've found the answer here: 我在这里找到了答案:

SQL Server Compact Edition 4.0: Error: 26 - Error Locating Server/Instance Specified SQL Server Compact Edition 4.0:错误:26-查找指定的服务器/实例时出错

SQL Compact needs SqlCeConnection objects, not SqlConnection !!! SQL Compact需要SqlCeConnection对象,而不是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 ;) 希望能帮助到你 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL错误:26-查找指定的服务器/实例时出错 - SQL error:26 - Error Locating Server/Instance Specified C#-错误26-定位指定的服务器/实例 - C#-Error 26- Locating Server/Instance Specified SQL服务器错误(提供程序:SQL网络接口,错误:26-错误指定服务器/实例的位置) - SQL SERVER ERROR (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) ASP.Net错误26错误定位指定的服务器/实例 - ASP.Net Error 26 Error Locating Server/Instance Specified 错误 26 - 定位服务器/指定实例时出错 - error 26- error locating server/instance specified 错误26的解决方案-指定错误的服务器/实例定位? - Solution for Error 26 - Error Locating Server/Instance Specified? 仅在调试时才出现SQL Server错误26(指定错误的服务器/实例) - SQL Server Error 26 (Error Locating Server/Instance Specified) Only When Debugging SQL Server Compact Edition 4.0:错误:26 - 找到指定的服务器/实例时出错 - SQL Server Compact Edition 4.0: Error: 26 - Error Locating Server/Instance Specified SQL Server EXPRESS:错误:26-在安装后指定服务器/实例时出错 - SQL Server EXPRESS: Error: 26 - Error Locating Server/Instance Specified after setup 错误26:错误定位生产环境中指定的服务器/实例-SQL Server 2014 WPF WCF - Error 26: Error Locating Server/Instance Specified in production environment - SQL server 2014 wpf wcf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM