【问题标题】:C# SqlCommand Connection.Open() issueC# SqlCommand Connection.Open() 问题
【发布时间】:2011-03-28 14:42:57
【问题描述】:

我有一个 C# ASP.NET Web 应用程序,我正在尝试使用数据库表中的一列填充 ASP:DropDownList。

我的代码:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Configuration;

namespace CRM2Sage
{
    public partial class SOPOrderEntry : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Fill1();
        }

        public void Fill1()
        {
            SqlCommand cmd = new SqlCommand("SELECT * FROM Products", new SqlConnection(WebConfigurationManager.AppSettings["CRM2Sage"]));
            //cmd.Connection.Open();
            cmd.Connection.Open();

            SqlDataReader ddlValues;
            ddlValues = cmd.ExecuteReader();

            vproduct.DataSource = ddlValues;
            vproduct.DataValueField = "theName";
            vproduct.DataTextField = "theName";
            vproduct.DataBind();

            cmd.Connection.Close();
            cmd.Connection.Dispose();
        }
    }
}

当我运行页面时出现以下错误

ConnectionString 属性有 未初始化。

指向cmd.Connection.Open();我不明白为什么,SQL连接存储在我的web.config文件中。

web.config

<?xml version="1.0"?>
<configuration>

    <appSettings />
    <connectionStrings>
      <add name="CRM2Sage" connectionString="Data Source=W2003CRMDEMO; Initial Catalog=CRM2Sage; User ID=newSA; Password=password;" providerName="System.Data.SqlClient" />
    </connectionStrings>

    <system.web>
        <compilation debug="true">

        </compilation>
        <!--
            The <authentication> section enables configuration 
            of the security authentication mode used by 
            ASP.NET to identify an incoming user. 
        -->
        <authentication mode="Windows" />
        <!--
            The <customErrors> section enables configuration 
            of what to do if/when an unhandled error occurs 
            during the execution of a request. Specifically, 
            it enables developers to configure html error pages 
            to be displayed in place of a error stack trace.

        <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
        -->
    </system.web>
</configuration>

谁能帮忙?

干杯

贾斯汀

【问题讨论】:

    标签: c# sql-server connection-string


    【解决方案1】:

    您需要检索.ConnectionString 属性:

    string connectionString = WebConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString;
    
    using(SqlConnection _con = new SqlConnection(connectionString))
    using(SqlCommand cmd = new SqlCommand("SELECT * FROM Products", _con))
    {
       // do your stuff here
    }
    

    您现在正在做的只是以CRM2Sage 的名称检索&lt;connectionStrings&gt; 下的整个条目。

    【讨论】:

      【解决方案2】:

      问题是,您正在访问AppSettings,但您想访问连接字符串:

      new SqlConnection(WebConfigurationManager.ConnectionStrings.ConnectionStrings["CRM2Sage"].ConnectionString)
      

      【讨论】:

        【解决方案3】:

        你可以使用

        System.Web.Configuration.WebConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString;
        
        Or
        System.Configuration.ConfigurationManager.ConnectionStrings["CRM2Sage"].ConnectionString;
        

        在 asp.net 中。但不是

        WebConfigurationManager.AppSettings["CRM2Sage"]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-09-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-11
          • 2017-04-02
          • 1970-01-01
          相关资源
          最近更新 更多