简体   繁体   English

C#访问多个数据库的最佳方法

[英]c# Best way to access multiple databases

I am creating ac# executable to run as a scheduled task. 我正在创建ac#可执行文件以作为计划任务运行。 The .exe is going to access multiple databases and update the same table and columns on each. .exe将访问多个数据库,并在每个数据库上更新相同的表和列。 The databases are split up by location. 数据库按位置拆分。 What is the best way to access multiple databases? 访问多个数据库的最佳方法是什么? Should I: 我是不是该:

  • Put the connections in the app.config and maybe use Entity Framework to access each database? 将连接放在app.config中,也许可以使用实体框架访问每个数据库?
  • Put the connection information in a .txt file and then use the SQL library to open connection, make update, close connection? 将连接信息放入.txt文件中,然后使用SQL库打开连接,进行更新,关闭连接?

No matter what you chose to use Entity Framework or ADO.NET always do put your connection strings in App.Config or Web.config file for better maintainability. 无论您选择使用Entity Framework还是ADO.NET都始终将连接字符串放在App.ConfigWeb.config文件中,以实现更好的可维护性。

So that, tomorrow if needed to change the connection string; 这样,明天如果需要更改连接字符串; you know there is only once place to change this. 您知道只有一次可以更改此设置。

Again, changing the config file *.config file doesn't need your code to be rebuild and redeployed to production environment. 同样,更改配置文件*.config文件不需要重新生成代码并将其重新部署到生产环境中。

Something like this would be recommended? 这样的东西会被推荐吗?

<connectionStrings>
<add name="Db1" connectionString="Data Source=SQL.*****;Initial Catalog=Db1;Persist Security Info=True;User ID=****;Password=****;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
<add name="Db2" connectionString="Data Source=SQL.*****;Initial Catalog=Db2;Persist Security Info=True;User ID=****;Password=****;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
<add name="Db3" connectionString="Data Source=SQL.*****;Initial Catalog=Db3;Persist Security Info=True;User ID=****;Password=****;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

 </connectionStrings>

I don't know if there is an absolute best way to create this kind of setup. 我不知道是否有绝对最佳的方法来创建这种设置。 It seems like you have a handle on things. 看来您对事物有把握。 I don't think speed or (any kind of) performance is an issue here. 我认为速度或(任何一种)性能都不是这里的问题。 I'd say, whatever is easier for you to maintain, would be 'best'. 我想说,任何对您而言更容易维护的都是“最佳”。 I say this only because these kinds of things usually start simple, and over time, grow in complexity. 我之所以这么说,是因为这类事情通常从简单开始,随着时间的推移,变得越来越复杂。

Good luck! 祝好运!

What I ended up doing is putting the constant sql string connection variables in the app.config under appSettings. 我最后要做的是将常量sql字符串连接变量放在appSettings下的app.config中。 And then I created a csv file where I put the catalogs(databases). 然后,我创建了一个csv文件,在其中放置了目录(数据库)。 This allows me to add databases to the csv file when a new one needs to be created. 这使我可以在需要创建一个新数据库时将数据库添加到csv文件中。 Then I read the file into an array and loop through it and only needing to change the catalog name in the connection string when going from one database to another. 然后,我将文件读入数组并循环遍历,从一个数据库到另一个数据库时,只需更改连接字符串中的目录名称。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM