Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SOOOO Frustrated...

Status
Not open for further replies.

mlager

Programmer
Sep 1, 2006
74
0
0
US
Am I the only one that needs to be able to allow a WinForms application to modify and SAVE an application-scope connection string at runtime? Seriously though, am I the only one who needs to do this, because apparently the only way to do it is edit the stupid XML app.config file, which is great for us IT people, but if I want to deploy my app to a non-technical user and allow them to point it to their DB, I can't tell them to go edit XML.

I get the whole "application settings are read-only" but isn't there some sort of override? Ughh.

Any advice?
 
I've also found that :) See the comment that I left on that page (by Rock-Pond Solutions)... That method works great, but only persists for that runtime. I could load a new connection string into a user setting and then set the runtimeconnectionstring() to the user setting when the app loads, but then each user would need to configure a string, and in a terminal server environment, I really need to be able to set it from the app at a global level.
 
Hmmm, what about setting up a seperate Global XML or text file to hold the connection string. Then after the conection string has been declared each user can grab the string from that file and use it to set the runtimeconnection string?
 
Thats what I had thought about doing... But one of my main goals is to be able to use .NET's protect functionality to encrypt the connection string once it is set, which I can only do on the app.config. I'm working something up that I think is working and I'll post it here if it does. Appreciate your support.
 
Glad to try and help, my only other thought is to work around .net and try to modify the app.config directly but I'm not found of that idea.
 
Here is something I did which I haven't seen on any documention on the web... I'm thinking about putting it into a doc page for other people since I'm sure it may be useful. Not sure if it is 100% yet but preliminary results seem to be good. This code will build a string, remove the old string, and then add the new string. It requires a reload of the application. It's the first time I've been able to get an app to work with the connection string inside the app.config file at run-time. Let me know your thoughts.

Dim builder As New MySql.Data.MySqlClient.MySqlConnectionStringBuilder
builder.Server = textbox_server.Text
builder.UserID = textbox_username.Text
builder.Password = textbox_password.Text
builder.Database = textbox_database.Text
builder.PersistSecurityInfo = False
builder.ConnectionTimeout = 5

Dim config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)
config.ConnectionStrings.ConnectionStrings.Remove("TKSEM.My.MySettings.ConnectionString")
config.ConnectionStrings.ConnectionStrings.Add(New ConnectionStringSettings("TKSEM.My.MySettings.ConnectionString", builder.ToString(), "MySql.Data.MySqlClient"))
config.Save()
 
Here is what I ended up with. The application deploys with a blank connection string. The user will then need to define a connection string for the application to work. The connection string will be added to app.config and then encrypted. The user can then go in and modify the string which will be encrypted as well.

'Build the MySQL connection string based on user input
Dim builder As New MySql.Data.MySqlClient.MySqlConnectionStringBuilder
builder.Server = textbox_server.Text
builder.UserID = textbox_username.Text
builder.Password = textbox_password.Text
builder.Database = textbox_database.Text
builder.PersistSecurityInfo = False
builder.ConnectionTimeout = 5

'Define config and section variables
Dim config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None)
Dim section = config.GetSection("connectionStrings")

'Unprotect connection string section
If (section.SectionInformation.IsProtected) = True Then
section.SectionInformation.UnprotectSection()
End If
section.SectionInformation.ForceSave = True
config.Save(System.Configuration.ConfigurationSaveMode.Modified)

'Remove old connection string, then add the new connection string that was built above
config.ConnectionStrings.ConnectionStrings.Remove("TKSEM.My.MySettings.ConnectionString")
config.ConnectionStrings.ConnectionStrings.Add(New ConnectionStringSettings("TKSEM.My.MySettings.ConnectionString", builder.ToString(), "MySql.Data.MySqlClient"))
config.Save()

'Protect connection string section
If (section.SectionInformation.IsProtected) = False Then
section.SectionInformation.ProtectSection(Nothing)
End If
section.SectionInformation.ForceSave = True
config.Save(System.Configuration.ConfigurationSaveMode.Modified)
 
I realize this is an old thread, but I am wondering about the statements:
mlager said:
Am I the only one that needs to be able to allow a WinForms application to modify and SAVE an application-scope connection string at runtime? Seriously though, am I the only one who needs to do this, because apparently the only way to do it is edit the stupid XML app.config file
Why does it have to be stored in the app.config file? Why can't it be in some text file, or the registry, or any other place you can fetch it from later?

A connection string is just another piece of information, and as a programmer it is up to me to decide where I would like to keep that information.

mlager said:
But one of my main goals is to be able to use .NET's protect functionality to encrypt the connection string once it is set, which I can only do on the app.config.
Why couldn't you just encrypt it in your code, and store that in your text file, registry, etc.?
 
Agreed, you certainly could do that... But if the connection string is stored as a "connectionstring" type in app.config, then I can use that in my table adapters and in visual studio's tools. If it were stored in a text file, i still wouldn't be able to set the applications official connection string to the contents of the text file because it would be read only. My method I showed above was really all I found. I'm probably not fully understanding your view.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top