How can i read connection string from web.config file in to a public class in class library. i've tried
如何从web读取连接字符串。配置文件到类库中的公共类中。我试过了
WebConfigurationManager
ConfigurationManager
but these are not recognized in class library
但是这些在类库中是不能识别的
12 个解决方案
#1
153
Add System.Configuration
as a reference.
添加系统。配置作为参考。
For some bizarre reason it's not included by default.
出于某种奇怪的原因,默认情况下不包括它。
#2
452
You need to add a reference to System.Configuration
and then use:
您需要向系统添加一个引用。配置,然后使用:
System.Configuration.ConfigurationManager.
ConnectionStrings["connectionStringName"].ConnectionString;
#3
57
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
#4
20
Add System.Configuration
as a reference then:
添加系统。配置作为参考:
using System.Configuration;
...
string conn =
ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString;
#5
16
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;
#6
11
In VB
: This should work
在VB中:应该可以
ConfigurationManager.ConnectionStrings("SQLServer").ConnectionString
In C#
it would be (as per comment of Ala)
在c#中,它将是(根据Ala的评论)
ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString
#7
9
using System.Configuration;
string conn = ConfigurationManager.ConnectionStrings["ConStringName"].ToString();
#8
8
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:
然后您可以使用此方法返回要传递给sqlconnection对象的连接字符串,以继续您的工作,如下所示:
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>
#9
5
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
以下网络>配置文件的代码
<connectionStrings>
<add name="ABCD" connectionString="Data Source=DESKTOP-SU3NKUU\MSSQLSERVER2016;Initial Catalog=TESTKISWRMIP;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
In the ablove Code ABCD is the Connection Name
在ablove代码中ABCD是连接名。
#10
4
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],因为您可能需要全局机器配置和可移植性
#11
2
First add this:
第一次添加:
using System.Configuration;
#12
1
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.
安装之后,您将看到一个提示符,提示符告诉您它所在的程序集,并询问您是否希望它添加所需的using语句。
#1
153
Add System.Configuration
as a reference.
添加系统。配置作为参考。
For some bizarre reason it's not included by default.
出于某种奇怪的原因,默认情况下不包括它。
#2
452
You need to add a reference to System.Configuration
and then use:
您需要向系统添加一个引用。配置,然后使用:
System.Configuration.ConfigurationManager.
ConnectionStrings["connectionStringName"].ConnectionString;
#3
57
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
#4
20
Add System.Configuration
as a reference then:
添加系统。配置作为参考:
using System.Configuration;
...
string conn =
ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString;
#5
16
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;
#6
11
In VB
: This should work
在VB中:应该可以
ConfigurationManager.ConnectionStrings("SQLServer").ConnectionString
In C#
it would be (as per comment of Ala)
在c#中,它将是(根据Ala的评论)
ConfigurationManager.ConnectionStrings["SQLServer"].ConnectionString
#7
9
using System.Configuration;
string conn = ConfigurationManager.ConnectionStrings["ConStringName"].ToString();
#8
8
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:
然后您可以使用此方法返回要传递给sqlconnection对象的连接字符串,以继续您的工作,如下所示:
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>
#9
5
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
以下网络>配置文件的代码
<connectionStrings>
<add name="ABCD" connectionString="Data Source=DESKTOP-SU3NKUU\MSSQLSERVER2016;Initial Catalog=TESTKISWRMIP;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
In the ablove Code ABCD is the Connection Name
在ablove代码中ABCD是连接名。
#10
4
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],因为您可能需要全局机器配置和可移植性
#11
2
First add this:
第一次添加:
using System.Configuration;
#12
1
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.
安装之后,您将看到一个提示符,提示符告诉您它所在的程序集,并询问您是否希望它添加所需的using语句。