[英]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.