2012-11-01 40 views
1

我正在使用configurationmanager.appsetting,并且在cn.Open行中出现错误。无法连接到asp.net中的SQL Server数据库

Web表单代码如下:

public partial class admin_CheckDocuments : System.Web.UI.Page 
{ 
    SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["con"]); 
    string sql; 
    string str; 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     cn.Open(); 
     str = Request.QueryString["taxid"]; 

     if (!IsPostBack) 
     { 
      filldata(); 
     } 
    } 

web.config是:

<configuration> 
    <connectionStrings/> 
    <appSettings> 
     <add key="con" 
      value="Data Source=EGGG\SKDB;Initial Catalog=tax;Integrated Security=true"/> 
    </appSettings> 

的错误是

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)

+5

什么是错误? – Robert

+0

错误是{“建立与SQL Server的连接时发生网络相关或实例特定的错误。服务器未找到或无法访问。验证实例名称是否正确,并将SQL Server配置为允许远程连接。 (提供程序:SQL网络接口,错误:26 - 定位服务器/实例指定的错误)“} – sisodia1991

+0

@ user1792379您是否有一个运行Sql Server的本地实例或一个在指定位置(EGGG \ SKDB)运行的实例? – Robert

回答

2

我在连接字符串中注意到了这一点:

Integrated Security=true

这意味着您的代码将尝试使用运行asp.net进程的服务器帐户的凭据进行连接。这通常是本地帐户,如网络服务,本地服务或IIS_IUSRS。您可以更改此设置,但默认情况下,您甚至无权登录到数据库。

5

引发此问题的原因可能是服务器名称错误,禁用的远程连接 和防火墙阻止。

请问您是否可以按照以下步骤解决此问题?

·检查运行SQL Server的服务器是否可以访问。你可以使用ping命令来测试。例如,ping **或ping 。 ping命令可能被防火墙阻止,请确保防火墙中启用了ICMP。 More info **

·选择合适的协议

·配置Windows防火墙,因此根据您选择使用什么协议。有关如何将Windows防火墙配置为允许SQL Server的详细信息,请More info

·启用SQL Server浏览器服务

您需要启用SQL Server浏览器服务如果下列都为真:

1.SQL Server没有监听默认的1433端口,也没有使用默认管道名称\。\ pipe \ sql \ query;

2.未在连接字符串中指定相应的TCP端口或管道名称(如Srv1 \ SQL2008,1500)。

如果您已启用SQL Server Browser Services,则仍需要打开Windows防火墙中浏览器服务使用的UDP 1434端口。

让我知道如果得到解决