2012-12-07 33 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配置爲允許遠程連接。 (provider:命名管道提供程序,error:40 - 無法打開到SQL Server的連接)

da.Fill(DS, 「標籤」);

此行

+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(); 
     } 

*編輯*

如果已經解決了這個問題,你需要仔細檢查你的連接字符串,因爲它沒有被正確解析。

一個簡單的方法就是到VS Studio的服務器資源管理器中手動添加一個到數據庫的連接。測試連接以查看是否解決問題。如果有,則可以從數據連接的屬性選項卡複製連接字符串。

希望這會有所幫助。

+0

仍是不working..I打開連接字符串.. – user766397

+0

你得到同樣的錯誤? – 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); 
相關問題