连接字符串:从配置文件转换为代码
Connection string: convert from config file to in code
我在从主应用程序和 class 库获取 Entity Framework 连接字符串时遇到了一些问题。我的主应用程序(*.exe)和我的 class 库(我想与 Excel 一起使用的 COM *.dll)都需要创建到 SQL 的连接服务器 CE 数据库或 SQL 服务器数据库。
目前,我在配置文件中定义了两个 'base' 连接字符串,一个用于 SQL 服务器 CE 实现,一个用于 SQL 服务器实现。这适用于我的主应用程序,因为它从配置文件(SQL Server CE 或 SQL Server)读取适当的 'base' 连接字符串,我调整连接字符串值(例如通过 'data source=' 获取 SQL 服务器 CE 的文件位置或 SQL 服务器的数据库名称),我很高兴。然而,这对我的 class 库来说效果不佳,因为配置文件取决于使用该库的应用程序。因此,我想通过在代码中而不是在配置文件中定义我的连接字符串来完全摆脱我的配置文件依赖性。
当前配置文件配置如下:
<add name="ConnectionCE" providerName="System.Data.SqlServerCe.4.0" connectionString="" />
<add name="ConnectionServer" providerName="System.Data.SqlClient" connectionString="" />
然后我创建上下文如下:
var context = new DbContext("name=ConnectionCE")
//Do things with the context above here, e.g. change the connection string value.
如何转换 "ConnectionCE" 和 "ConnectionServer" 连接字符串,这样我就不必依赖配置文件,并且可以直接在我的 C# 代码中创建它们(使用正确的供应商)?
谢谢!
DbContext(string)
构造函数接受名称或连接字符串。
您可以简单地在代码中构建连接字符串并将其传递给构造函数。
您可以使用 System.Data.SqlClient.SqlConnectionStringBuilder
class 为 SQL 服务器建立连接字符串。我认为,对于 SQL Server CE 连接字符串,很容易使用 string.Format()
.
SQL 服务器的代码如下所示:
var connStr = new SqlConnectionStringBuilder
{
InitialCatalog = "YourDb",
IntegratedSecurity = true,
DataSource = @".\SQLServer",
};
var db = new DbContext(connStr.ConnectionString);
应为 SQL 服务器 CE 指定提供程序,因此代码如下所示:
var conn = DbProviderFactories.GetFactory("System.Data.SqlServerCe.4.0").CreateConnection();
conn.ConnectionString = "DataSource = \"C:\ExistingDBFile.db\"";
var db = new DbContext(conn, true);
我在从主应用程序和 class 库获取 Entity Framework 连接字符串时遇到了一些问题。我的主应用程序(*.exe)和我的 class 库(我想与 Excel 一起使用的 COM *.dll)都需要创建到 SQL 的连接服务器 CE 数据库或 SQL 服务器数据库。
目前,我在配置文件中定义了两个 'base' 连接字符串,一个用于 SQL 服务器 CE 实现,一个用于 SQL 服务器实现。这适用于我的主应用程序,因为它从配置文件(SQL Server CE 或 SQL Server)读取适当的 'base' 连接字符串,我调整连接字符串值(例如通过 'data source=' 获取 SQL 服务器 CE 的文件位置或 SQL 服务器的数据库名称),我很高兴。然而,这对我的 class 库来说效果不佳,因为配置文件取决于使用该库的应用程序。因此,我想通过在代码中而不是在配置文件中定义我的连接字符串来完全摆脱我的配置文件依赖性。
当前配置文件配置如下:
<add name="ConnectionCE" providerName="System.Data.SqlServerCe.4.0" connectionString="" />
<add name="ConnectionServer" providerName="System.Data.SqlClient" connectionString="" />
然后我创建上下文如下:
var context = new DbContext("name=ConnectionCE")
//Do things with the context above here, e.g. change the connection string value.
如何转换 "ConnectionCE" 和 "ConnectionServer" 连接字符串,这样我就不必依赖配置文件,并且可以直接在我的 C# 代码中创建它们(使用正确的供应商)?
谢谢!
DbContext(string)
构造函数接受名称或连接字符串。
您可以简单地在代码中构建连接字符串并将其传递给构造函数。
您可以使用 System.Data.SqlClient.SqlConnectionStringBuilder
class 为 SQL 服务器建立连接字符串。我认为,对于 SQL Server CE 连接字符串,很容易使用 string.Format()
.
SQL 服务器的代码如下所示:
var connStr = new SqlConnectionStringBuilder
{
InitialCatalog = "YourDb",
IntegratedSecurity = true,
DataSource = @".\SQLServer",
};
var db = new DbContext(connStr.ConnectionString);
应为 SQL 服务器 CE 指定提供程序,因此代码如下所示:
var conn = DbProviderFactories.GetFactory("System.Data.SqlServerCe.4.0").CreateConnection();
conn.ConnectionString = "DataSource = \"C:\ExistingDBFile.db\"";
var db = new DbContext(conn, true);