简体   繁体   中英

c# Best way to access multiple databases

I am creating ac# executable to run as a scheduled task. The .exe is going to access multiple databases and update the same table and columns on each. 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?
  • Put the connection information in a .txt file and then use the SQL library to open connection, make update, close connection?

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.

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.

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. And then I created a csv file where I put the catalogs(databases). This allows me to add databases to the csv file when a new one needs to be created. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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