Im trying to build a program that runs on a database. I have tryed everything i can think of to change my connection string at runtime. What i want to do is enable a user to select a local SQL server that containes a copy of my database, and save it to the application settings. i keep getting a read only message when i try to change the app settings connectionstring setting. im not sure if i worded that right, if you need more information, please let me know.
Thank you,
Andrew

VB Application Settings
Keyboard_Cowboy
We've solved a similar issue with the use of a Data Acces Object (DAO).
What this does is creates an object similar to the following...
string ServerName
public ExecuteNonQuery(StoredProcedureName, ParameterList) as Boolean
public GetDataFromStoredProcedure(StoredProcedureName, ParameterList, DataSet) as Boolean
private _getConnection(SqlConnection) as Boolean
private _getCommand(SqlCommand) as Boolean
private _getData(DataSet, Parameters, SPName) as Boolean
private _executeNonQuery(SPName,Parameters) as Boolean
Ok - so we build the CS on the fly, using the supplied parameters which can be obtained from the calling application.
e.g.
MyDAO.ServerName = "SQL50"
MyDAO.DatabaseName = "MyDatabase"
In the calling application we have a custom XML file that can be read/written with the database server name such as "SQL50\SQL001" and database name such as "MyApplicationDatabase" in an entry something like this...
<CustomApplicationSettings>
<ApplicationSetting>
<key>ServerName</key>
<value>SQL50\SQL001</value>
</ApplicationSetting>
<ApplicationSetting>
<key>DatabaseName</key>
<value>MyApplicationDatabase</value>
</ApplicationSetting>
</ CustomApplicationSettings>
And read the value from the XML node for each setting into the DAO. This means you can read/write as you wish. Your not leaving any particularly sensitive info open, since the passwords, usernames etc. shouldn't be in here. We use a pass through of the identity to authenticate which works for us.
JDL440
I have allready achieved this in VS 2005 and VS 2003.
I have written a function that replaces a certain value in the XML configuration file by reading teh stream of the original file, replacing it by the new value, and saving that stream with the exact same filename of the configuration file.
I'm using this to alter my database connection at runtime, and do not seem to have unpleasant side-effects. BUT this requires a restart of the application!!
My connectionstring in the XML:(i gave the important keys a colour so you can see where i search for them in the code)
<connectionStrings>
<add name="SphinxTrace.Settings.pvsConn" connectionString="Driver={Pervasive ODBC Client Interface};ServerName=SPHINX-KDME;dbq=C:\Belgosuc" />
<add name="SphinxTrace.Settings.sqlConn" connectionString="Data Source=.;Initial Catalog=SQLDeClercq;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
The code that works for the above xml snippet:
Public Sub ReplaceConfigSettings(ByVal FName As String, ByVal key As String, ByVal val As String)
Dim xDoc As New Xml.XmlDocument
Dim xNode As Xml.XmlNode
On Error GoTo ErrorHandler
xDoc.Load(Application.StartupPath & "\" & FName)
For Each xNode In xDoc("configuration")("connectionStrings")
End If Next'save the altered stream as a file with the exact name of current config file
xDoc.Save(Application.StartupPath & "\" & FName)
ExitHandler:
xDoc =
Nothing Exit SubErrorHandler:
MsgBox(
"Check configuration replace method", MsgBoxStyle.Information) Resume ExitHandler End Sub------------------------------------------------------------------------
I hope this works for you, it doesn't gave me any problems!
Truly yours,
Kenny De Maertelaere
Junior .NET developer
H. Feijt
I found a link on the forums:
https://blogs.msdn.com/smartclientdata/archive/2005/07/25/443034.aspx
Basically I had a situation where I wanted to change the connectionstring that M$ store when using the funky project data sources way of coding.
All I did is added this sub to the settings.vb file (instructions how to view the setting code in included in that link above)
Private
Sub MySettings_SettingsLoaded(ByVal sender As Object, ByVal e As System.Configuration.SettingsLoadedEventArgs) Handles Me.SettingsLoaded Me.Item("cnDatabase") = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Me.Item("UserSettingDatabaseLocation") End SubWhere cnDatabase is the name of the Connection you have used for your datasets, and UserSettingDatabaselocation was just a User setting that i have used, but you could set that to anything like your own XML based setting or anything.
Good Luck!
Kaishain
You can surely modify the generated dataset code (xxxdataset.designer.xsd) to suit your need.
The initConeection function can be modified to take your own connection string.
Private Sub InitConnection() Me._connection = New System.Data.SqlClient.SqlConnection Me._connection.ConnectionString = Global.WindowsApplication6.My.MySettings.Default.NorthwindConnectionString End Sub
However, I do not recommend to do so as the file will be regenerated if there is a change in the dataset. Use carefully and on your own risk.
HTH.
Niky
Thank you,
Andrew
Stephie
There are two types of application settings, based on scope:
Application-scoped settings can be used for information such as a URL for a Web service or a database connection string. These values are associated with the application, so users cannot change them at run time.
User-scoped settings can be used for information such as remembering the last position of a form or a font preference. Users can change these values at run time.
You can change the type of a setting using the Scope property.
See details in http://msdn2.microsoft.com/en-us/library/a65txexh.aspxNote that the ConnectionStringSetting is ApplicationScoped setting and so you can never change it at run time.
Hope this help.