简体   繁体   English

连接字符串:从配置文件转换为代码

[英]Connection string: convert from config file to in code

I'm having some trouble getting my Entity Framework connection strings working from my main application and from my class library. 我在从主应用程序和类库中获取实体框架连接字符串时遇到了一些麻烦。 Both my main application (an *.exe) and my class library (a COM *.dll that I want to use with Excel) will need to create a connection to either a SQL Server CE database or a SQL Server database. 我的主应用程序(* .exe)和类库(我想与Excel一起使用的COM * .dll)都需要创建与SQL Server CE数据库或SQL Server数据库的连接。

At the moment, I define two 'base' connection strings in the config file, one for the SQL Server CE implementation and one for the SQL Server implementation. 目前,我在配置文件中定义了两个“基本”连接字符串,一个用于SQL Server CE实施,一个用于SQL Server实施。 This works fine for my main application, as it reads the appropriate 'base' connection string from the config file (either SQL Server CE or SQL Server), I adjust the connection string value (eg file location via 'data source=' for SQL Server CE or database name for SQL Server), and I'm good to go. 这对于我的主应用程序工作正常,因为它会从配置文件(SQL Server CE或SQL Server)中读取适当的“基本”连接字符串,我调整了连接字符串值(例如,通过SQL的“数据源=”来定位文件位置) Server CE或SQL Server的数据库名称),我很高兴。 This however doesn't work well for my class library, as the config file depends on the application that is using the library. 但是,这对于我的类库来说效果不佳,因为配置文件取决于使用该库的应用程序。 I'd like to therefore get rid of my config file dependency completely, by defining my connection strings in code as opposed to in the config file. 因此,我想通过在代码中而不是在配置文件中定义我的连接字符串来完全摆脱对配置文件的依赖。

The current config file configuration looks as follows: 当前的配置文件配置如下所示:

<add name="ConnectionCE" providerName="System.Data.SqlServerCe.4.0" connectionString="" />

<add name="ConnectionServer" providerName="System.Data.SqlClient" connectionString="" />

I then create the context as follows: 然后,我按如下方式创建上下文:

var context = new DbContext("name=ConnectionCE")
//Do things with the context above here, e.g. change the connection string value.

How can I convert the "ConnectionCE" and "ConnectionServer" connection strings so that I don't have to rely on the config file, and can create these directly in my C# code (with the correct provider)? 如何转换“ ConnectionCE”和“ ConnectionServer”连接字符串,以使我不必依赖配置文件,并且可以直接在C#代码中创建它们(使用正确的提供程序)?

Thanks! 谢谢!

DbContext(string) constructor accepts name or connection string. DbContext(string)构造函数接受名称或连接字符串。

You can simply build the connection string in code and pass it into the constructor. 您只需在代码中构建连接字符串,然后将其传递给构造函数即可。

You can use System.Data.SqlClient.SqlConnectionStringBuilder class to build connection string for SQL Server. 您可以使用System.Data.SqlClient.SqlConnectionStringBuilder类为SQL Server建立连接字符串。 I think, for SQL Server CE connection string, it is easy to use string.Format() . 我认为,对于SQL Server CE连接字符串,很容易使用string.Format()

The code for SQL Server will look like: SQL Server的代码如下所示:

var connStr = new SqlConnectionStringBuilder
{
    InitialCatalog = "YourDb",
    IntegratedSecurity = true,
    DataSource = @".\SQLServer",
};
var db = new DbContext(connStr.ConnectionString);

Provider should be specified for SQL Server CE, so the code will look like next: 应该为SQL Server CE指定提供程序,因此代码如下所示:

var conn = DbProviderFactories.GetFactory("System.Data.SqlServerCe.4.0").CreateConnection();
conn.ConnectionString = "DataSource = \"C:\\ExistingDBFile.db\"";
var db = new DbContext(conn, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从Config文件中读取连接字符串? - How to read Connection String from a Config file? 无法从配置文件中读取连接字符串 - Unable to read connection string from Config File 从外部配置文件读取连接字符串 - Reading connection string from external config file 如何比较打开连接字符串与配置文件中的连接字符串 - How to compare open connection string with connection string from config file 从后面的代码永久更改app.config中的连接字符串 - Permanently change connection string in app.config from code behind EF Code First MigrateDatabaseToLatestVersion接受来自config的连接字符串Name - EF Code First MigrateDatabaseToLatestVersion accepts connection string Name from config 来自ASP.NET Web配置文件的SSRS连接字符串 - SSRS connection string from asp.net web config file 如何在 web.config 文件中写入连接字符串并从中读取? - How to write connection string in web.config file and read from it? 从 Azure Functions 中的配置文件加载连接字符串 - Load Connection String from Config File in Azure Functions 将配置文件中的连接字符串注入 EF Core DbContext - Injecting Connection String from config file into EF Core DbContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM