【问题标题】:C# connection string in web.config fileweb.config 文件中的 C# 连接字符串
【发布时间】:2010-11-05 17:24:18
【问题描述】:

在我的后台代码中,如何访问存储在我的 web.config 文件中的连接字符串?

【问题讨论】:

    标签: c# .net visual-studio-2008 web-config connection-string


    【解决方案1】:
    System.Web.Configuration.WebConfigurationManager.ConnectionStrings["YouConnStringName"].ConnectionString;
    

    这需要引用 System.Configuration.dll 和 System.Web.dll。

    【讨论】:

    • 不应该是 System.Web.Configuration.WebConfigurationManager.ConnectionStrings...
    • 这给了我一个错误:Cannot apply indexing with [] to an expression of type 'System.Configuration.ConnectionStringSettingsCollection' 任何想法如何解决这个问题?
    • 不确定,但我猜你没有使用 System.Web.Configuration.WebConfigurationManager.ConnectionStrings["conString"].ConnectionString;或者也许你需要参考system.configuration?您收到该错误是因为您使用的集合没有实现 IList,可能只实现了 IEnumerable。
    【解决方案2】:
    【解决方案3】:

    来自http://msdn.microsoft.com/en-us/library/ms178411.aspx的评论

    string c = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["myconnectionstringname"].ConnectionString;
    

    要求您的项目引用 System.Web 和 System.Configuration。我实际上必须添加对“System.Configuration”的引用,而不仅仅是添加一个 Using。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多