[英]SQL error:26 - Error Locating Server/Instance Specified
我试图运行一个从远程sql服务器获取数据的程序,该程序适用于另一个远程站点的同事,但是当我运行该程序时,调试器说Sql_Reader返回null。
我可以ping并连接到远程sql服务器。 根据http://blogs.msdn.com/b/sql_protocols/archive/2007/05/13/sql-network-interfaces-error-26-error-locating- server-instance-specified.aspx但是,当我运行程序以从sql服务器获取数据时,出现错误并指出找不到或无法访问远程服务器。
我检查了每个错误的连接字符串:26-指定服务器/实例时出错。 (无法从主机服务器连接到本地Db)
这是GetConnectionString方法中的连接字符串。
return @"Data Source= 2.2.2.2\SQLEXPRESS; Persist Security Info=False;User ID=myusername;Password=mypassword;Initial Catalog=SomeDataStructure;";
在具有sql_Connection.Open()的行上发生以下错误:
SqlException未处理。 建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 服务器未找到或无法访问。 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (提供者:SQL网络接口,错误:26-指定服务器/实例时出错)
// Body of the method
str_Query = "SELECT " + dict_SearchData["FieldName"] + " FROM " + dict_SearchData["Table"] +
" WHERE " + dict_SearchData["IndexColumn"] + " = '" + dict_SearchData["IndexRow"] + "';";
using (SqlConnection sql_Connection = new SqlConnection(dict_SearchData["ConnectionString"]))
{
SqlCommand sql_Command = new SqlCommand(str_Query, sql_Connection);
sql_Connection.Open();
SqlDataReader sql_Reader = sql_Command.ExecuteReader();
try
{
sql_Reader.Read();
str_FieldValue = sql_Reader[0].ToString();
}
catch (System.InvalidOperationException)
{
//ignore the exception
}
finally
{
// Always call Close when done reading.
sql_Reader.Close();
}
}
我将端口号添加到连接字符串中,并且能够使用该应用程序远程连接到数据库。 我在逗号后面的IP地址后面添加了端口号。
return @"Data Source= 2.2.2.2,1433\SQLEXPRESS; Persist Security Info=False;User ID=myusername;Password=mypassword;Initial Catalog=SomeDataStructure;";
看来您的连接字符串可能包含多余的字符或格式错误。 尝试采取额外的步骤,将字符串放入本地字符串变量,然后将其分配给ConnectionString对象。 这样,您可以单步执行代码并离散检查字符串值。 另外,更常见的习惯是指定服务器名称而不是IP地址,尽管我认为两者都应该起作用。 同样,反斜杠可能会产生问题,可能必须转义该字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.