by matt
17. March 2009 21:14
When you are first playing with table adapters you might get to see. When you create your typed data set, Visual Studio will create an element in the generated XSD that contains the connection string that you were using. This will more than likely not be the connection string that you want to use in your production environment. So how do you go about changing it? Well, if you start using the intellisense in Visual Studio, you will soon find that there is a 'Connection' property on the table adapter once you have created it.
Great! Lets set the connection string on that and well be done. Ah, not so easy. This will compile just fine, but as Connection is in fact an internal member, it is only available within the same assembly. So if you are trying to access your data set from another assembly (which is often the case) you won't be able to set it.
I've seen a number of people asking for a way to resolve this, and luckily the solution is simple and can be resolved by taking the following steps:
- In the design view of the dataset, select the table adapter that you want to change the connection for.
- Open the properties of the Adapter (<F4> in Visual Studio) and change the Connection Modifier property to public.
That should do it, you should then be able to use something like the following to change your connection:
string myNewConnectionString = "Data Source=(local);Initial Catalog=MyStore;Integrated Security=True";
TestDataSetTableAdapters.OrderTableAdapter ta = new DataSetTests.TestDataSetTableAdapters.OrderTableAdapter();
ta.Connection.ConnectionString = myNewConnectionString;
Job done.