Point ADO.Net DataSet to different databases at runtime?

随声附和 提交于 2019-12-02 02:49:11

问题


I have a large ADO.Net dataset and two database schemas (Oracle) with different constraints. The dataset will work with either schema, but I want to be able to tell the dataset which schema to use (via connection string) at runtime.

Is that even possible?


回答1:


In the .Net 2.0 world, you can change your connection string on your table adapters at run-time. You just have to be sure the Connnection property is public, which can be set from the dataset designer.




回答2:


Datasets don't know what database they're pointing to -- they're just containers for data. If the dataset is filled with a data adapter, then as @Austin Salonen pointed out, you change that on the adapter side.




回答3:


This is a code snippet on how you could updated the connection string at runtime. It does not matter what generated the dataset.

            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);


来源:https://stackoverflow.com/questions/121350/point-ado-net-dataset-to-different-databases-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!