我正在使用SQL Server 2008作为数据库服务器在C#.net中使用Windows应用程序。以下是我的app.config文件中的代码:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="myconnection" value="Data Source=ritesh-pc\SQLEXPRESS;Initial Catalog=dbname;Integrated Security=SSPI"/>
</appSettings>
</configuration>
每当我尝试访问该密钥时
"myconnection" as string connectionString = ConfigurationSettings.AppSettings["myconnection"];
发生以下错误
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
答案 0 :(得分:1)
由于密码/等原因,这可能是您的连接字符串错误/无法访问。
答案 1 :(得分:1)
答案 2 :(得分:1)
你想要像
这样的东西string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
请参阅Store connection string in web.config我觉得这篇文章非常有用
答案 3 :(得分:0)
之前我遇到了同样的错误,我做的是:
How to resolve Error 26 in SQL Server?
我尝试使用(服务器的IP地址)\ SQLEXPRESS。有时实例名称无法识别,最好使用sql server的IP地址。
或者你的连接字符串错了。在app.config中检查语法是否如下所示:
<add name="conString"
connectionString="Data Source=10.99.89.80;Initial Catalog=EWB_FileDownloader;User ID = sa; Password = 12435@"
providerName="System.Data.SqlClient" />
答案 4 :(得分:0)
问题在于连接字符串,我认为你必须设置你的帐户参数(如果有的话)
Data Source=.\SQLEXPRESS;Initial Catalog=attendence;Integrated Security=SSPI;
User ID=myUsername;Password=myPassword;
有关详细信息:请查看this