2012-12-07 3 views
1

데이터베이스와 상호 작용할 때이 코드가 표시됩니다.SQL Server에 연결하는 동안 네트워크 관련 또는 인스턴스 관련 오류가 발생했습니다.

내 Web.config의이 연결 문자열이 포함되어

<?xml version="1.0"?> 
<configuration> 
<connectionStrings> 
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/> 
<add name="WebNewYearConnectionString" connectionString="Data Source=XXXX; Initial Catalog=XXXXX; User ID=XXXXX; Password=XXXXXX;"  providerName="System.Data.SqlClient"/> 
</connectionStrings> 
<system.web> 
<compilation debug="true" targetFramework="4.0"/> 
<customErrors mode="Off"/> 
<authentication mode="Forms"> 
    <forms loginUrl="~/Account/Login.aspx" timeout="2880"/> 
</authentication> 
<membership> 
    <providers> 
    <clear/> 
    <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/> 
    </providers> 
</membership> 
<profile> 
    <providers> 
    <clear/> 
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/> 
    </providers> 
</profile> 
<roleManager enabled="false"> 
    <providers> 
    <clear/> 
    <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/"/> 
    <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/"/> 
    </providers> 
</roleManager> 
</system.web> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
</system.webServer> 
<system.data> 
<DbProviderFactories> 
    <remove invariant="System.Data.SqlServerCe.4.0"/> 
    <add name="Microsoft SQL Server Compact Edition Client Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact Edition Client 4.0" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/> 
</DbProviderFactories> 
</system.data> 

내 코드가

xSqlConnections con = new SqlConnections("System.Configuration.ConfigurationManager.ConnectionStrings["WebNewYearConnectionString"].ConnectionString"); 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     string Clientname = Request.QueryString["name"]; 
     string str = "select * from Client_Detail where Client_Name='" + Clientname + "'"; 
     DataTable dat; 
     SqlDataAdapter da = new SqlDataAdapter(sql, con); 
     DataSet ds = new DataSet(); 
     da.Fill(ds, "tab"); 
     DataTable dat = new DataTable(); 
     dat = ds.Tables["tab"]; 
     EventName.Text = dat.Rows[0]["Event_Name"].ToString(); 
     Event_Description.Text = dat.Rows[0]["Description"].ToString(); 
     Event_Date.Text = dat.Rows[0]["Date"].ToString(); 
     Entry_Fee.Text = dat.Rows[0]["Price"].ToString(); 
     Event_Client.Text = dat.Rows[0]["Client_Name"].ToString(); 
     Event_Location.Text = dat.Rows[0]["Event_Location"].ToString(); 
     Session["Event_Name"] = EventName.Text; 


    } 

을 포함하고 내 코드는이 오류

에게 네트워크를주고있다 SQL Serv에 연결하는 동안 관련 또는 인스턴스 관련 오류가 발생했습니다. 어. 서버를 찾을 수 없거나 액세스 할 수 없습니다. 인스턴스 이름이 올 바르고 SQL Server가 원격 연결을 허용하도록 구성되어 있는지 확인하십시오. ("탭"DS)

da.Fill에서

- (공급자 : 명명 된 파이프 공급자가 오류 (40)는 SQL Server에 대한 연결을 열 수 없습니다)

이 줄

+3

당신은 – Derek

답변

0
xSqlConnections con = new SqlConnections("System.Configuration.ConfigurationManager.ConnectionStrings["WebNewYearConnectionString"].ConnectionString"); 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      con.open(); 

      string Clientname = Request.QueryString["name"]; 
      string str = "select * from Client_Detail where Client_Name='" + Clientname + "'"; 
      DataTable dat; 
      SqlDataAdapter da = new SqlDataAdapter(sql, con); 
      DataSet ds = new DataSet(); 
      da.Fill(ds, "tab"); 
      DataTable dat = new DataTable(); 
      dat = ds.Tables["tab"]; 
      EventName.Text = dat.Rows[0]["Event_Name"].ToString(); 
      Event_Description.Text = dat.Rows[0]["Description"].ToString(); 
      Event_Date.Text = dat.Rows[0]["Date"].ToString(); 
      Entry_Fee.Text = dat.Rows[0]["Price"].ToString(); 
      Event_Client.Text = dat.Rows[0]["Client_Name"].ToString(); 
      Event_Location.Text = dat.Rows[0]["Event_Location"].ToString(); 
      Session["Event_Name"] = EventName.Text; 

      con.Close(); 
     } 

* EDIT *

이 문제를 해결 한 경우, 당신은 제대로 해결되지 않는 연결 문자열을 재확인 할 필요가있다.

이 작업을 수행하는 가장 쉬운 방법은 VS Studio의 서버 탐색기로 이동하여 데이터베이스에 수동으로 연결을 추가하는 것입니다. 연결을 테스트하여 해결되는지 확인하십시오. 이 경우 데이터 연결의 속성 탭에서 연결 문자열을 복사 할 수 있습니다.

희망이 도움이됩니다.

+0

아직도이 working..I가 연결 문자열 ... – user766397

+0

이 같은 오류가나요 개방되지 않은 연결을 열어 havent 한? – Derek

+0

예 동일한 오류가 발생합니다 – user766397

0

실제로 코드에서 전체적으로 큰 따옴표가 있습니까? 그렇다면 그것은 당신의 문제입니다.

xSqlConnections con = new SqlConnections("System.Configuration.ConfigurationManager.ConnectionStrings["WebNewYearConnectionString"].ConnectionString"); 
             ^
+0

따옴표를 제거했지만 여전히 같은 오류가 발생합니다. – user766397

0
은 다음과 같이 연결 인스턴스를 변경

:

xSqlConnections con = new SqlConnections(System.Configuration.ConfigurationManager.ConnectionStrings["WebNewYearConnectionString"].ConnectionString); 
관련 문제