I want to know the ways to get connection string from web.config file in asp.net.
我想知道从web获取连接字符串的方法。配置文件在asp.net。
I just only know the below way .
我只知道下面的方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace Sherserve.DataAccessLayer
{
public class DBGateway
{
public static string conString;
public DBGateway()
{
conString = ConfigurationManager.ConnectionStrings["test"].ToString();
}
}
}
4 个解决方案
#1
35
Using the ConfigurationManager.ConnectionStrings
is about the only proper way, to use it properly with sanity check you can have such code:
使用ConfigurationManager。ConnectionStrings是唯一合适的方式,要正确地使用它并进行完整性检查,您可以拥有这样的代码:
public DBGateway()
{
ConnectionStringSettings mySetting = ConfigurationManager.ConnectionStrings["test"];
if (mySetting == null || string.IsNullOrEmpty(mySetting.ConnectionString))
throw new Exception("Fatal error: missing connecting string in web.config file");
conString = mySetting.ConnectionString;
}
This will throw useful error in case the connection string is missing, instead of cryptic "null object" error.
这将抛出有用的错误,以防连接字符串丢失,而不是神秘的“空对象”错误。
Worth to mention that the ConnectionStringSettings
class is overriding the ToString()
method:
值得一提的是,ConnectionStringSettings类覆盖了ToString()方法:
public override string ToString()
{
return this.ConnectionString;
}
So it means that using ConfigurationManager.ConnectionStrings["test"].ToString()
is the same like ConfigurationManager.ConnectionStrings["test"].ConnectionString
however you still better perform sanity check and personally it looks cleaner to use the actual property and not depend on the class to give it.
这就意味着使用configurationmanager . connectionstring ["test"]. tostring()和ConfigurationManager.ConnectionStrings["test"一样。但是,您仍然可以更好地执行完整性检查,而且在使用实际属性时,它看起来更干净,而不依赖于类来提供。
#2
8
Here is the whole solution:-
这是整个解决方案:-
string constring = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
SqlConnection con = new SqlConnection(constring);
DataSet ds = new DataSet();
try
{
SqlDataAdapter dataAdapter = new SqlDataAdapter(query, con);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
con.Open();
dataAdapter.Fill(ds, "table");
return ds;
}
catch (Exception ex)
{
}
finally
{
if (con.State == System.Data.ConnectionState.Open)
con.Close();
}
This is how you can fetch records from database into datatable.
这就是如何从数据库获取记录到datatable的方法。
Hope this is what you were looking for.
希望这就是你想要的。
#3
0
I have a method on my WebAssistant Class. i have a key on appSettings that contain my WebAppName, check the below line:
我对我的英语课有一个方法。我有一个包含我的WebAppName的appSettings的键,检查下面的行:
<add key="DOMAIN_NAME" value="mRizvandi.com"/>
and my connection name alwyas has "DomainName"+"DBConnectionString" template such as:
我的连接名alwyas有"DomainName"+"DBConnectionString"模板,如:
<add name="mRizvandiDBConnectionString" connectionString=...
Ok, everything are ready to get connectionstring without pass any string.
好的,所有东西都准备好了,不用传递任何字符串就能得到connectionstring。
public static string GetDBConnectionString()
{
string retValue = "";
string domainUrl = "";
string connectionKey = "";
string dbConnectionString = "";
domainUrl = GetDomainUrl();
connectionKey = domainUrl.Substring(0, domainUrl.IndexOf(".")) + "DBConnectionString";
dbConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[connectionKey].ToString();
retValue = dbConnectionString;
return retValue;
}
I hope would be helpful.
我希望能有所帮助。
#4
0
Following code works for me. I've added the exception in case connectionString not founded in web.config
下面的代码适合我。我在web.config中没有创建connectionString时添加了异常
web.config:
. config:
<configuration>
<connectionStrings>
<add name="DBConnection" connectionString="Data Source=myaddress;Initial Catalog=MyCatalog;User ID=myuser;Password=pass;Encrypt=True;TrustServerCertificate=False"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
In my connection class:
在我的连接类:
static internal string GetSqlConnectionString(){
try {
ConnectionStringSettings mySetting = ConfigurationManager.ConnectionStrings("DataBaseConnection");
if (mySetting == null)
throw new Exception("Database connection settings have not been set in Web.config file");
return mySetting.ConnectionString;
} catch (Exception ex) {
throw;
}}
#1
35
Using the ConfigurationManager.ConnectionStrings
is about the only proper way, to use it properly with sanity check you can have such code:
使用ConfigurationManager。ConnectionStrings是唯一合适的方式,要正确地使用它并进行完整性检查,您可以拥有这样的代码:
public DBGateway()
{
ConnectionStringSettings mySetting = ConfigurationManager.ConnectionStrings["test"];
if (mySetting == null || string.IsNullOrEmpty(mySetting.ConnectionString))
throw new Exception("Fatal error: missing connecting string in web.config file");
conString = mySetting.ConnectionString;
}
This will throw useful error in case the connection string is missing, instead of cryptic "null object" error.
这将抛出有用的错误,以防连接字符串丢失,而不是神秘的“空对象”错误。
Worth to mention that the ConnectionStringSettings
class is overriding the ToString()
method:
值得一提的是,ConnectionStringSettings类覆盖了ToString()方法:
public override string ToString()
{
return this.ConnectionString;
}
So it means that using ConfigurationManager.ConnectionStrings["test"].ToString()
is the same like ConfigurationManager.ConnectionStrings["test"].ConnectionString
however you still better perform sanity check and personally it looks cleaner to use the actual property and not depend on the class to give it.
这就意味着使用configurationmanager . connectionstring ["test"]. tostring()和ConfigurationManager.ConnectionStrings["test"一样。但是,您仍然可以更好地执行完整性检查,而且在使用实际属性时,它看起来更干净,而不依赖于类来提供。
#2
8
Here is the whole solution:-
这是整个解决方案:-
string constring = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;
SqlConnection con = new SqlConnection(constring);
DataSet ds = new DataSet();
try
{
SqlDataAdapter dataAdapter = new SqlDataAdapter(query, con);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
con.Open();
dataAdapter.Fill(ds, "table");
return ds;
}
catch (Exception ex)
{
}
finally
{
if (con.State == System.Data.ConnectionState.Open)
con.Close();
}
This is how you can fetch records from database into datatable.
这就是如何从数据库获取记录到datatable的方法。
Hope this is what you were looking for.
希望这就是你想要的。
#3
0
I have a method on my WebAssistant Class. i have a key on appSettings that contain my WebAppName, check the below line:
我对我的英语课有一个方法。我有一个包含我的WebAppName的appSettings的键,检查下面的行:
<add key="DOMAIN_NAME" value="mRizvandi.com"/>
and my connection name alwyas has "DomainName"+"DBConnectionString" template such as:
我的连接名alwyas有"DomainName"+"DBConnectionString"模板,如:
<add name="mRizvandiDBConnectionString" connectionString=...
Ok, everything are ready to get connectionstring without pass any string.
好的,所有东西都准备好了,不用传递任何字符串就能得到connectionstring。
public static string GetDBConnectionString()
{
string retValue = "";
string domainUrl = "";
string connectionKey = "";
string dbConnectionString = "";
domainUrl = GetDomainUrl();
connectionKey = domainUrl.Substring(0, domainUrl.IndexOf(".")) + "DBConnectionString";
dbConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings[connectionKey].ToString();
retValue = dbConnectionString;
return retValue;
}
I hope would be helpful.
我希望能有所帮助。
#4
0
Following code works for me. I've added the exception in case connectionString not founded in web.config
下面的代码适合我。我在web.config中没有创建connectionString时添加了异常
web.config:
. config:
<configuration>
<connectionStrings>
<add name="DBConnection" connectionString="Data Source=myaddress;Initial Catalog=MyCatalog;User ID=myuser;Password=pass;Encrypt=True;TrustServerCertificate=False"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
In my connection class:
在我的连接类:
static internal string GetSqlConnectionString(){
try {
ConnectionStringSettings mySetting = ConfigurationManager.ConnectionStrings("DataBaseConnection");
if (mySetting == null)
throw new Exception("Database connection settings have not been set in Web.config file");
return mySetting.ConnectionString;
} catch (Exception ex) {
throw;
}}