Set the connection string in code

SteveInBeloit

Well-known member
Joined
May 22, 2006
Messages
132
Programming Experience
10+
Hi,
I have seen this discussed before, but still don't really have what I want.
If I need a connection string while in code, I use
My.Settings.connectionStringName
seems to work fine.

But when I take it to the client, I have to remember to change the app. right before the last build, which I may mispell.

I would like to make a form that takes the Server name and password, and then change it in code. But I don't know what to change to make it work.

Does this sound do-able?
Thanks
 
Yeah I've got something like this. My app is used in 3 locations, UK, Australia and Thailand, so I have a splash screen asking the user what location they are in.

This will work if when you first made a connection to your server, you said to keep the connection string stored in your app to use for all connections.


Firstly you need to change the connection from Application to User, and from (Connection String) to String.

To-Do
a) open My Project and click the Settings tab
b) copy the connection string (all of it) that appears as the Value
c) drop down Scope and change to User
d) drop down Type and change to String
e) delete everything that appears in Value, and paste your connection string from (b)

image3xg7.jpg


Now you've got this, there is something to note
DO NOT EDIT YOUR DATASET(s) that use this connection!!!!

If you need to add a new DataTable, or edit one, or create a new query etc etc, you need to go back to My Project Settings and change the Type to (Connection String), and set the value to your saved string again.
Once done, press Synchronise up the top and save.

You can now edit your dataset(s). If you accidently try to make a change without changing the connection string back, you'll get an error. Close the dataSet designer and say NO to save changes. Close Visual Studio and reopen.


With that out the way, and Type set to String, you can now set your connection string in code.

On my splash screen I have 3 radio buttons in a group box. The code is set so that (only showing relevant code);
VB.NET:
If Me.rbAustralia.Checked = True Then
   
   My.Settings.SalesNPDConnectionString = "Data Source=UK-BAN-APP1;Initial Catalog=SalesNPD_Aus;Integrated Security=True"

ElseIf Me.rbUK.Checked = True Then

   My.Settings.SalesNPDConnectionString = "Data Source=UK-BAN-APP1;Initial Catalog=SalesNPD_UK;Integrated Security=True"

ElseIf Me.rbThailand.Checked = True Then

   My.Settings.SalesNPDConnectionString = "Data Source=UK-BAN-APP1;Initial Catalog=SalesNPD_Thai;Integrated Security=True"
                
End If

All 3 databases are on the same server. All 3 have EXACTLY the same schema in terms of table names and table column names.
The only change is the data, which is relevant to each site.

The splash screen just sets the correct database. The code above is condensed down, I actually go further and use Active Directory groups to define who from each location can view the other locations.

That hopefully should get you started, as I say the one thing to be wary about is editing datasets after changing the connection Type.

good luck :D
 
Application scope ones are, User scope ones arent. The dataset designer can only work with (connection string) type entries, and those entries can only be (Application scope) so I cannot underline enough what Arg81 said:

If you forget to change the setting back to App Scope/Conn String BEFORE you edit ANYTHING about your dataset, you WILL shaft up the dataset completely. If you then save the shafted set, you will spend a considerable number of hours getting it right again

I really really recommend you only perform this procedure when you have finished dataset development. BACKUP your project BEFORE you do this.
 
Thank you for the warnings. I believe I will steer away from this approach. No offense Arg81, but I am so new to this that I would probably forget or not understand completely and mess up my project.
Thanks for the input.
 
Going on that, I found a good approach to this.

If you leave it as a User String, and you go to get to edit the dataset, you get an error. Close the dataset editor and just say NO to saving. Then edit the "My Project" Settings and click Synchronise. You can now open the dataset editor again and it'll be fine.

However, as Cjard pointed out - major problems arise if you say YES to saving, however the error that comes up will make you realise not to click YES to saving.

I was new to this when I first found out how to do it - in .net 1.1 it was a lot simplier to edit the dataAdapters Connection string on each form!! You get use to what needs to be done after a while. I'm currently working on 4 apps, and I need to take this approach on only 1 project. Yet I always remember this and make sure I've set the Settings to the correct scope before Editing dataset(s) or saving and uploading the project.
 
Back
Top