使用 table 个适配器动态连接到数据库

Connecting to database dynamically with table adapters

我有一个在设计时使用数据集创建 table 适配器的应用程序。这与特定的数据库相关联。我的要求是,我希望能够连接到与前一个具有相同架构的不同数据库。我应该能够在运行时实现这一点。无论如何我可以做到吗?或者使用 TableAdapter 的最佳替代方法是什么?

干杯, 哈里斯

是的,您可以更改适配器指向的数据库,方法是关闭现有连接,更改连接字符串,然后打开新连接。

//Work with existing connection...

tableAdapter1.Connection.Close();
tableAdapter1.Connection.ConnectionString = "Your new DB connection string";
tableAdapter1.Connection.Open();

//Work with same adapter but now pointing to new DB specified in above string.