.net 3.5:要从app.config读取connectionstring吗?
如何使用.net api从app.config文件读取连接字符串信息 平台是.NET3.5.net 3.5:要从app.config读取connectionstring吗?,.net,connection,app-config,.net,Connection,App Config,如何使用.net api从app.config文件读取连接字符串信息 平台是.NET3.5 <?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add connectionString="" providerName="" name=""/>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add connectionString="" providerName="" name=""/>
</connectionStrings>
</configuration>
如果
name
是表示连接字符串名称的字符串值:
var connectionString =
ConfigurationManager.ConnectionStrings[name].ConnectionString;
在您的示例中,您没有为name
提供一个值,因此您必须先这样做,然后它才能工作。在配置中:
<add name="ConnectionName" connectionString="Data Source=xxxx\yyyy;Initial Catalog=MyDB;User ID=userName;Password=pwd" />
更好的做法是定义一个函数并在代码中使用它:
public string getConnectionStringMyDB()
{
return ConfigurationManager.ConnectionStrings["ConnectionName"].ToString();
}
请参阅(原始文件被删除时,在回程机器上)
您可能需要向系统添加程序集引用。配置以下是我所做的
我需要一个服务来自动启动并连接到SQLServer数据库,作为启动的一部分。这意味着数据库连接字符串的名称需要存储在注册表中,并且存储在注册表中的字符串必须对应于定义的连接字符串。答案是一个小型Winforms小程序,它管理服务启动参数的注册表存储,其中一个存储的参数是DB连接字符串的名称
我向Linq创建的数据库上下文类添加了两个静态函数。一种方法枚举DB项目的设置部分中定义的DB连接名称。第二个方法从数据库连接名称中返回一个数据库上下文。注册表管理小程序调用枚举器方法以填充列表框,windows服务调用GetDBContextFromConnectionName()
方法以将从注册表检索到的DB连接名转换为DB上下文。然后,DB上下文用于DB访问
这两个方法被放入我添加到项目中的一个类文件中,该类文件与Linq创建的datacontext类同名
结果是:
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
namespace RepositoryProject
{
public partial class RepositoryDataContext
{
/// <summary>
/// Return a MS SQL-LINQ DB Context given the name of the DB Connection name defined in
/// Properties.Settings.Default area of the SQL-Linq project.
/// </summary>
/// <param name="dbConnectionName">The name of the prediefined DB Connection string</param>
/// <returns>A SQL-Linq database context </returns>
public static RepositoryDataContext GetDBContextFromConnectionName(string dbConnectionName)
{
string fullConnectionString = null;
dbConnectionName = dbConnectionName.Trim();
if (!String.IsNullOrEmpty(dbConnectionName))
{
SettingsPropertyCollection allConnectionStrings = global::Cognex.TA.Framework.Properties.Settings.Default.Properties;
SettingsProperty connectionProperty = allConnectionStrings[dbConnectionName];
if (null != connectionProperty)
{
fullConnectionString = (string) connectionProperty.DefaultValue;
if (String.IsNullOrEmpty(dbConnectionName))
{
string msg = "";
msg += String.Format( "The connection string name, {0}, exists within the settings of the RepositoryDataContext class but creates an empty DB connection string.", dbConnectionName);
throw new ArgumentException(msg);
}
}
else
{
string msg = "";
msg += String.Format( "The connection string name, {0}, does not exist within the settings of the RepositoryDataContext class.", dbConnectionName);
throw new ArgumentException(msg);
}
}
else
{
string msg = "";
msg += "The connection string name to the test repository cannot be null or empty.";
throw new ArgumentException(msg);
}
return new RepositoryDataContext(fullConnectionString);
}
/// <summary>
/// Return a list of all the DB Connection names defined in
/// Properties.Settings.Default area of the SQL linq project.
/// </summary>
/// <returns>A list of DB Connection name</returns>
public static List<string> GetAllDBConnectionNames()
{
List<string> listONames = new List<string>();
/*
* within the the Linq-generated code (TestRepository.designer.cs) there is an empty constructor for
* the data context which looks similar to this:
*
* public TestRepositoryDataContext() :
* base(global::Framework.Properties.Settings.Default.DefaultConnectionString, mappingSource)
* {
OnCreated();
* }
*
* Duplicate that assembly name here
*/
SettingsPropertyCollection allConnectionStrings = global::Framework.Properties.Settings.Default.Properties;
foreach(SettingsProperty entry in allConnectionStrings)
{
if (entry.PropertyType.ToString().Equals("System.String"))
{
listONames.Add(entry.Name);
}
}
return listONames;
}
}
}
使用系统;
使用系统配置;
使用System.Collections.Generic;
使用系统集合;
命名空间RepositoryProject
{
公共部分类RepositoryDataContext
{
///
///根据中定义的数据库连接名称的名称返回MS SQL-LINQ数据库上下文
///SQL Linq项目的Properties.Settings.Default区域。
///
///预定义的数据库连接字符串的名称
///sqllinq数据库上下文
公共静态RepositoryDataContext GetDBContextFromConnectionName(字符串dbConnectionName)
{
字符串fullConnectionString=null;
dbConnectionName=dbConnectionName.Trim();
如果(!String.IsNullOrEmpty(dbConnectionName))
{
SettingsPropertyCollection AllConnectionString=global::Cognex.TA.Framework.Properties.Settings.Default.Properties;
SettingsProperty connectionProperty=AllConnectionString[dbConnectionName];
if(null!=connectionProperty)
{
fullConnectionString=(字符串)connectionProperty.DefaultValue;
if(String.IsNullOrEmpty(dbConnectionName))
{
字符串msg=“”;
msg+=String.Format(“连接字符串名称{0}存在于RepositoryDataContext类的设置中,但创建了一个空的DB连接字符串。”,dbConnectionName);
抛出新的ArgumentException(msg);
}
}
其他的
{
字符串msg=“”;
msg+=String.Format(“RepositoryDataContext类的设置中不存在连接字符串名称{0}。”,dbConnectionName);
抛出新的ArgumentException(msg);
}
}
其他的
{
字符串msg=“”;
msg+=“到测试存储库的连接字符串名称不能为null或空。”;
抛出新的ArgumentException(msg);
}
返回新的RepositoryDataContext(fullConnectionString);
}
///
///返回中定义的所有DB连接名称的列表
///SQL linq项目的Properties.Settings.Default区域。
///
///数据库连接名称的列表
公共静态列表GetAllDBConnectionNames()
{
列表名称=新列表();
/*
*在Linq生成的代码(TestRepository.designer.cs)中,有一个空构造函数用于
*与此类似的数据上下文:
*
*公共TestRepositoryDataContext():
*基本(全局::Framework.Properties.Settings.Default.DefaultConnectionString,mappingSource)
* {
OnCreated();
* }
*
*在此处复制该程序集名称
*/
SettingsPropertyCollection AllConnectionString=global::Framework.Properties.Settings.Default.Properties;
foreach(在所有连接字符串中设置属性条目)
{
if(entry.PropertyType.ToString().Equals(“System.String”))
{
添加(entry.Name);
}
}
返回列表名;
}
}
}
我希望这能有所帮助。我已经到处搜索,找不到您参考的教程的更新链接。。你有更新的链接吗?@ecoe我在网上找到了
ConnectionStringSettings connection = ConfigurationManager.ConnectionStrings["MyConnectionString"]
string connectionString = connection.ConnectionString
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Collections;
namespace RepositoryProject
{
public partial class RepositoryDataContext
{
/// <summary>
/// Return a MS SQL-LINQ DB Context given the name of the DB Connection name defined in
/// Properties.Settings.Default area of the SQL-Linq project.
/// </summary>
/// <param name="dbConnectionName">The name of the prediefined DB Connection string</param>
/// <returns>A SQL-Linq database context </returns>
public static RepositoryDataContext GetDBContextFromConnectionName(string dbConnectionName)
{
string fullConnectionString = null;
dbConnectionName = dbConnectionName.Trim();
if (!String.IsNullOrEmpty(dbConnectionName))
{
SettingsPropertyCollection allConnectionStrings = global::Cognex.TA.Framework.Properties.Settings.Default.Properties;
SettingsProperty connectionProperty = allConnectionStrings[dbConnectionName];
if (null != connectionProperty)
{
fullConnectionString = (string) connectionProperty.DefaultValue;
if (String.IsNullOrEmpty(dbConnectionName))
{
string msg = "";
msg += String.Format( "The connection string name, {0}, exists within the settings of the RepositoryDataContext class but creates an empty DB connection string.", dbConnectionName);
throw new ArgumentException(msg);
}
}
else
{
string msg = "";
msg += String.Format( "The connection string name, {0}, does not exist within the settings of the RepositoryDataContext class.", dbConnectionName);
throw new ArgumentException(msg);
}
}
else
{
string msg = "";
msg += "The connection string name to the test repository cannot be null or empty.";
throw new ArgumentException(msg);
}
return new RepositoryDataContext(fullConnectionString);
}
/// <summary>
/// Return a list of all the DB Connection names defined in
/// Properties.Settings.Default area of the SQL linq project.
/// </summary>
/// <returns>A list of DB Connection name</returns>
public static List<string> GetAllDBConnectionNames()
{
List<string> listONames = new List<string>();
/*
* within the the Linq-generated code (TestRepository.designer.cs) there is an empty constructor for
* the data context which looks similar to this:
*
* public TestRepositoryDataContext() :
* base(global::Framework.Properties.Settings.Default.DefaultConnectionString, mappingSource)
* {
OnCreated();
* }
*
* Duplicate that assembly name here
*/
SettingsPropertyCollection allConnectionStrings = global::Framework.Properties.Settings.Default.Properties;
foreach(SettingsProperty entry in allConnectionStrings)
{
if (entry.PropertyType.ToString().Equals("System.String"))
{
listONames.Add(entry.Name);
}
}
return listONames;
}
}
}