在运行时将ADO.Net DataSet指向不同的数据库吗?
时间:2020-03-06 14:36:08 来源:igfitidea点击:
我有一个大型的ADO.Net数据集和两个具有不同约束条件的数据库架构(Oracle)。数据集可以使用任何一种模式,但是我希望能够在运行时告诉数据集要使用哪种模式(通过连接字符串)。
那有可能吗?
解决方案
在.Net 2.0世界中,我们可以在运行时更改表适配器上的连接字符串。我们只需要确保Connnection属性是公共属性即可,可以从数据集设计器中进行设置。
数据集不知道它们指向的数据库-它们只是数据的容器。如果数据集中填充了数据适配器,则正如@Austin Salonen指出的那样,我们可以在适配器端进行更改。
这是一个有关如何在运行时更新连接字符串的代码段。生成数据集无关紧要。
DataSet ds = new DataSet(); // Do some updateing here // Put your connection string here dyanmiclly System.Data.OleDb.OleDbCommand command = new System.Data.OleDb.OleDbCommand("Your Runtime Connection String"); // Create the data Adapter System.Data.OleDb.OleDbDataAdapter dataAdapter = new System.Data.OleDb.OleDbDataAdapter(command); // Update the dataset dataAdapter.Update(ds);