1

由于我有很多数据库,我想遍历它们,对它们中的每一个执行 linq 查询/更新。

是否可以做类似的事情

foreach(var r in master.sysdatabases)
{
from b from r.chicken
select b.age;
}

我有跨数据库支持的高级版。

4

2 回答 2

5

如果你所有的数据库都有一个相同的“鸡”表,你可以这样做:

var builder = new SqlConnectionStringBuilder (Connection.ConnectionString);

foreach (var db in sys.Databases)
{
   builder.InitialCatalog = db.Name;
   var dc = new TypedDataContext (builder.ToString());
   try
   {
      var query =
         from b in dc.Chickens
         select b.Age;

      query.Dump();
   }
   catch { ... }
}
于 2012-07-14T00:31:11.500 回答
0

I was able to solve it like this, but I honestly don't like using string concatenated queries.

var r = (from b in Sysdatabases select b.Name).ToList();
foreach(var i in r)
{
try{
    var o = ExecuteQuery<string>("select urls from "+i+".dbo.website_setting");
    Console.WriteLine(o);
}
catch(Exception){}
}

the try,execute is for if the table does not exist (master database etc...)

于 2012-07-13T16:20:04.643 回答