Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Data Source using Dynamic Connection String
 

Data Source using Dynamic Connection String

I'm using the Application Settings to declare a default connection string. Then I created a data source that uses that connection string. The problem is that I can not change the connection sting because it has application scope.
When the app starts, it reads a few registry values and constructs the connection string. Further, I have a "Settings" display that the user can use to change the settings. Is it possible to declare my Connection String as a string in the application settings ? Is the data source going to resolve it correctly ? I don't mind if using the changed connection string requires the application to restart.
What would the recommended course of action be ?

Ventsislav Velev  Tuesday, July 28, 2009 3:52 PM
Hi Ventsislav,

The connectionString which generated by designer can't be changed at runtime due to it uses the application scope. This kind of questions have been raised many times. You can take a look at the following discussions.

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/1ce2d4a6-3afc-4dbc-aaf3-563035b627d5/
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/7483b816-be7a-4204-a4d3-cfb14b2aae26

Based on my experience, I think you can choose to edit the app.config file by using notepad, or XML APIs to write the setting to the config file at runtime which you can find sample code from this thread .

>Also, I have a string variable inside my code that keeps the currently used connection string. The question thus becomes how do I force the data set to use that connection >string?

You can change the ConnectionString of the Connection property of the TableAdapter object before you use the TableAdapter object to fill the dataset.

this.testTableAdapter.Connection.ConnectionString = "current connection string";
this.testTableAdapter.Fill(this.testDataSet.Food);

If you have anything unclear, please feel free to let me know.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Wednesday, July 29, 2009 4:15 AM
Also, I have a string variable inside my code that keeps the currently used connection string. The question thus becomes how do I force the data set to use that connection string?
Ventsislav Velev  Tuesday, July 28, 2009 5:31 PM
Hi Ventsislav,

The connectionString which generated by designer can't be changed at runtime due to it uses the application scope. This kind of questions have been raised many times. You can take a look at the following discussions.

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/1ce2d4a6-3afc-4dbc-aaf3-563035b627d5/
http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/7483b816-be7a-4204-a4d3-cfb14b2aae26

Based on my experience, I think you can choose to edit the app.config file by using notepad, or XML APIs to write the setting to the config file at runtime which you can find sample code from this thread .

>Also, I have a string variable inside my code that keeps the currently used connection string. The question thus becomes how do I force the data set to use that connection >string?

You can change the ConnectionString of the Connection property of the TableAdapter object before you use the TableAdapter object to fill the dataset.

this.testTableAdapter.Connection.ConnectionString = "current connection string";
this.testTableAdapter.Fill(this.testDataSet.Food);

If you have anything unclear, please feel free to let me know.

Best regards,
Bruce Zhou
Please mark the replies as answers if they help and unmark if they don't.
Bruce.Zhou  Wednesday, July 29, 2009 4:15 AM
Hi Ventsislav,

How's your problem now? If the problem is still not solved or any further help from me, please feel free to let me know.

Best regards,
Bruce Zhou
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Bruce.Zhou  Friday, July 31, 2009 1:17 AM
What I ended up doing is the following:
1. I created a String variable in my code that contains the dynamically generated connection string and a SQLConnection object that uses the connection string to connect to the data source.
2. All my other forms are using that connection object for their table adapters.

I still don't allow "on the fly" connection switching. If you change the settings affecting the connection string, you have the restart the app to see the change taking place.
Ventsislav Velev  Monday, August 03, 2009 5:09 PM

Hi Ventsislav ,

Thank you for sharing your final decision. If you have further problems or any concerns, feel free to contact me.

Best regards,

Bruce Zhou

MSDN Subscriber Support in Forum

If you have any feedback on our support, please contact msdnmg@microsoft.com


Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Bruce.Zhou  Tuesday, August 04, 2009 2:23 AM

You can use google to search for other answers

Custom Search

More Threads

• Passing Bindingsource to Details Form with ComboBox
• DataGridView row height
• Listview Columnclick event doesn't fire
• passing parameters to stored procedure
• Binding datagridview to stored procedure
• Searching for specific text data in the datagridview control
• Add row to datagridview
• DataGridViewTextBoxColumn, override Paint problem
• Graphic artifacts when horizontally scrolling DataGridView
• How to show a default text when no rows is bounded in a grid?