简体   繁体   English

从 web.config 读取连接字符串

[英]Read connection string from web.config

How can I read a connection string from a web.config file into a public class contained within a class library?如何将web.config文件中的连接字符串读取到包含在 class 库中的公共 class 中?

I've tried:我试过了:

WebConfigurationManager

ConfigurationManager

But these classes are not recognized within my class library.但是这些类在我的 class 库中无法识别。

You need to add a reference to System.Configuration and then use:您需要添加对System.Configuration的引用,然后使用:

System.Configuration.ConfigurationManager.
    ConnectionStrings["connectionStringName"].ConnectionString;

Add System.Configuration as a reference.添加System.Configuration作为参考。

For some bizarre reason it's not included by default.出于某种奇怪的原因,默认情况下它不包括在内。

C# C#

// Add a using directive at the top of your code file    
using System.Configuration;

// Within the code body set your variable    
string cs = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;

VB VB

' Add an Imports statement at the top of your code file    
Imports System.Configuration

' Within the code body set your variable    
Dim cs as String = ConfigurationManager.ConnectionStrings("connectionStringName").ConnectionString

Add System.Configuration as a reference then:然后添加System.Configuration作为参考:

 using System.Configuration;

 ...

 string conn = 
    ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString;

I guess you need to add a reference to the System.Configuration assembly if that have not already been added.我猜您需要添加对System.Configuration程序集的引用(如果尚未添加)。

Also, you may need to insert the following line at the top of your code file:此外,您可能需要在代码文件的顶部插入以下行:

using System.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.DataVisualization.Charting;
using System.Web.UI.WebControls;  

C# C#

string constring = ConfigurationManager.ConnectionStrings["ABCD"].ConnectionString;
                using (SqlConnection con = new SqlConnection(constring))

BELOW WEB.CONFIG FILE CODE WEB.CONFIG 文件代码下方

<connectionStrings>
    <add name="ABCD" connectionString="Data Source=DESKTOP-SU3NKUU\MSSQLSERVER2016;Initial Catalog=TESTKISWRMIP;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>

In the above Code ABCD is the Connection Name在上面的代码 ABCD 是连接名称

In VB : This should workVB中:这应该有效

ConfigurationManager.ConnectionStrings("SQLServer").ConnectionString

In C# it would be (as per comment of Ala)C#中(根据 Ala 的评论)

ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString

You have to invoke this class on the top of your page or class:您必须在页面顶部或 class 上调用此 class:

using System.Configuration;

Then you can use this Method that returns the connection string to be ready to passed to the sqlconnection object to continue your work as follows:然后,您可以使用此方法返回连接字符串以准备传递给 sqlconnection object 继续您的工作,如下所示:

    private string ReturnConnectionString()
    {
       // Put the name the Sqlconnection from WebConfig..
        return ConfigurationManager.ConnectionStrings["DBWebConfigString"].ConnectionString;
    }

Just to make a clear clarification this is the value in the web Config:只是为了明确说明,这是 web 配置中的值:

  <add name="DBWebConfigString" connectionString="....." />   </connectionStrings>
using System.Configuration;


string conn = ConfigurationManager.ConnectionStrings["ConStringName"].ToString();
using System.Configuration;


string connString = ConfigurationManager.ConnectionStrings["ConStringName"].ToString();

Remember don't Use ConnectionStrings[index] because you might of Global machine Config and Portability记住不要使用 ConnectionStrings[index] 因为你可能会使用 Global machine Config and Portability

First add this:首先添加这个:

using System.Configuration;

Everybody seems to be suggesting that adding每个人似乎都建议添加

using System.Configuration;

which is true.这是真的。

But might I suggest that you think about installing ReSharper's Visual Studio extension?但是我可以建议您考虑安装 ReSharper 的 Visual Studio 扩展吗?

With it installed, instead of seeing an error that a class isn't defined, you'll see a prompt that tells you which assembly it is in, asking you if you want it to add the needed using statement.安装它后,您不会看到 class 未定义的错误,而是会看到一个提示,告诉您它在哪个程序集中,询问您是否希望它添加所需的 using 语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM