C# entity framework for循环动态连接字符串

C# entity framework for loop dynamic connection string

我是一名 DBA,并决定作为一个项目来帮助我学习 c# / ef 以开发uild 一个监控 sql 服务器的应用程序,我的想法是开发一个 windows 服务运行以使用 .net quartz 中的计划作业从各​​种 sql 实例收集所有统计信息。即来自各种动态管理视图的连接用户和详细信息。

存储配置数据,即要监控的服务器我正在使用 sql 数据库,因此它只包含 table 个要监控的服务器,您将能够通过网络添加移动 ui.

现在我遇到的问题是如何遍历 EF 中的服务器 "table" 并获取连接字符串列并使用它连接到各种 sql 实例以获取统计信息。 (这些稍后会写回数据库,供前端分析)

e.g.configurationtable数据:
服务器名:测试服务器
连接字符串:testserver\inst1

希望一切都有意义,感谢您的宝贵时间

您可以像这样创建所有连接字符串键的列表-

List<String> DataVaseKeys = new List<String>(); DataVaseKeys.Add("testserver\inst1");
 DataVaseKeys.Add("testserver\inst2");
  foreach (var key in DataVaseKeys)  {

    string currentConString=System.Configuration.ConfigurationManager.
        ConnectionStrings[key].ConnectionString;

    //access to the data base with your connection string here 
 }