C# WinForms changing database name in other form -
so here first question , first c# program:
i need function permanently change connection string.
my program has structure: main form form1
, when click button, options
, new - form3
, user can log in (password protects changing options) , if login successfull create new form - form4
. in constructor of form4
, pass sqlconnection
object form1
. want change database name clicking on button, here code:
var config = configurationmanager .openexeconfiguration(configurationuserlevel.none); var connectionstringssection = (connectionstringssection)config .getsection("connectionstrings"); var x = connectionstringssection .connectionstrings["app.properties.settings.ppsconnectionstring"] .connectionstring; string[] words = x.split(';'); words[1] = "initial catalog="+textboxdb.text; x=string.join(";", words); connectionstringssection .connectionstrings["app.properties.settings.ppsconnectionstring"] .connectionstring = x; //above, variable x in debug mode looks right //i read line below should update file, change permamently config.save(configurationsavemode.modified); configurationmanager.refreshsection("connectionstrings");
but doesn't change anything, , have no idea how change settings file user change database name , when restart app, should have new changed connection string.
edit :
thanks responses turns out should run code bin/release .exe version, not under debug in vs , changes config file.
if using visual studio can set range of settings within executable using properties form of project , these settings can accessed without reflection. these properties can made 'application level' or 'user level' determining how wide ranging changes be.
yourprojectname.properties.settings.default.yoursettingname = "a setting";
this if know how many connectionstrings , other variables want store, if require set unknown number of connection strings use datatable rows each setting, can saved xml file in applications directory, or per user.
Comments
Post a Comment