1

当我想与数据库交互时,我得到了这个代码。

我的 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 Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供者:命名管道提供者,错误:40 - 无法打开与 SQL Server 的连接)

da.Fill(ds, "tab");

这条线

4

3 回答 3

0

你的代码中是否真的有整个事情的引号?如果是这样,那是你的问题。

xSqlConnections con = new SqlConnections("System.Configuration.ConfigurationManager.ConnectionStrings["WebNewYearConnectionString"].ConnectionString");
                                         ^
于 2012-12-07T16:36:14.833 回答
0

像这样更改您的连接实例:

xSqlConnections con = new SqlConnections(System.Configuration.ConfigurationManager.ConnectionStrings["WebNewYearConnectionString"].ConnectionString);
于 2013-01-29T08:54:31.207 回答
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 中的服务器资源管理器并手动添加到数据库的连接。测试连接以查看它是否解决。如果是这样,您可以从数据连接的属性选项卡中复制连接字符串。

希望这可以帮助。

于 2012-12-07T14:31:39.693 回答