从 web.config 读取连接字符串

发布于 2024-11-09 12:24:53 字数 172 浏览 3 评论 0原文

如何将 web.config 文件中的连接字符串读取到类库中包含的公共类中?

我已经尝试过了:

WebConfigurationManager

ConfigurationManager

但是这些类在我的类库中未被识别。

How can I read a connection string from a web.config file into a public class contained within a class library?

I've tried:

WebConfigurationManager

ConfigurationManager

But these classes are not recognized within my class library.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

緦唸λ蓇 2024-11-16 12:24:53

您需要添加对 System.Configuration 的引用,然后使用:

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

You need to add a reference to System.Configuration and then use:

System.Configuration.ConfigurationManager.
    ConnectionStrings["connectionStringName"].ConnectionString;
十年不长 2024-11-16 12:24:53

添加 System.Configuration 作为参考。

由于某些奇怪的原因,它默认不包含在内。

Add System.Configuration as a reference.

For some bizarre reason it's not included by default.

还给你自由 2024-11-16 12:24:53

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

' 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

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

' 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
慈悲佛祖 2024-11-16 12:24:53

添加 System.Configuration 作为参考,然后:

 using System.Configuration;

 ...

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

Add System.Configuration as a reference then:

 using System.Configuration;

 ...

 string conn = 
    ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString;
铁轨上的流浪者 2024-11-16 12:24:53

我想您需要添加对 System.Configuration 程序集的引用(如果尚未添加)。

此外,您可能需要在代码文件的顶部插入以下行:

using System.Configuration;

I guess you need to add a reference to the System.Configuration assembly if that have not already been added.

Also, you may need to insert the following line at the top of your code file:

using System.Configuration;
七堇年 2024-11-16 12:24:53
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#

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

WEB.CONFIG 文件代码下面的

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

在上面的代码中 ABCD 是连接名称

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#

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

BELOW WEB.CONFIG FILE CODE

<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

滴情不沾 2024-11-16 12:24:53

VB 中:这应该可以

ConfigurationManager.ConnectionStrings("SQLServer").ConnectionString

C# 中工作(根据 Ala 的评论)

ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString

In VB : This should work

ConfigurationManager.ConnectionStrings("SQLServer").ConnectionString

In C# it would be (as per comment of Ala)

ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString
森林迷了鹿 2024-11-16 12:24:53

您必须在页面或类的顶部调用此类:

using System.Configuration;

然后您可以使用此方法返回连接字符串,准备传递给 sqlconnection 对象以继续您的工作,如下所示:

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

只是为了明确说明,这是网络配置中的值:

  <add name="DBWebConfigString" connectionString="....." />   </connectionStrings>

You have to invoke this class on the top of your page or 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:

    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:

  <add name="DBWebConfigString" connectionString="....." />   </connectionStrings>
天暗了我发光 2024-11-16 12:24:53
using System.Configuration;


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


string conn = ConfigurationManager.ConnectionStrings["ConStringName"].ToString();
生生不灭 2024-11-16 12:24:53
using System.Configuration;


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

请记住不要使用 ConnectionStrings[index] 因为您可能会使用全局计算机配置和可移植性

using System.Configuration;


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

Remember don't Use ConnectionStrings[index] because you might of Global machine Config and Portability

吃不饱 2024-11-16 12:24:53

首先添加这个:

using System.Configuration;

First add this:

using System.Configuration;
耀眼的星火 2024-11-16 12:24:53

每个人似乎都在建议添加

using System.Configuration;

哪个是正确的。

但我是否可以建议您考虑安装 ReSharper 的 Visual Studio 扩展?

安装后,您不会看到未定义类的错误,而是会看到一条提示,告诉您该类位于哪个程序集中,询问您是否希望它添加所需的 using 语句。

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?

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.

哎呦我呸! 2024-11-16 12:24:53

添加命名空间 System.Configuration 作为参考

using System.Configuration;

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

Add Namespace System.Configuration as a reference then

using System.Configuration;

 string conn =  ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文