c# - Removing the hard coded value to app.config file -
i have developed form takes user user id , password , display list of databases available in local server.now have done in hard coded format...like this
public void binddbdropdown() { //create connection object sqlconnection sconnection = new sqlconnection( configurationsettings.appsettings["connectionstring"]); //to open connection. sconnection.open(); //query select list of databases. string selectdatabasenames = @"select name master..sysdatabases"; //create command object sqlcommand scommand = new sqlcommand(selectdatabasenames, sconnection); try { //create data set dataset sdataset = new dataset("master..sysdatabases"); //create dataadapter object sqldataadapter sdataadapter = new sqldataadapter(selectdatabasenames, sconnection); sdataadapter.tablemappings.add("table", "master..sysdatabases"); //fill dataset sdataadapter.fill(sdataset); //bind database names in combobox dataviewmanager dsv = sdataset.defaultviewmanager; //provides master mapping between sourcr table //and system.data.datatable cmbdatabases.datasource = sdataset.tables["master..sysdatabases"]; cmbdatabases.displaymember = "name"; cmbdatabases.valuemember = ("name"); } catch(exception ex) { //all exceptions handled , written in eventlog. eventlog logexception = new eventlog("application"); logexception.source = "mfdbanalyser"; logexception.writeentry(ex.message); } { //if connection not closed close connection if(sconnection.state != connectionstate.closed) { sconnection.close(); } } } /// <summary> ///this function binds names of tables primary ///keys in dropdown cmbresults. /// </summary> public void getprimarykeytable() { //an instance of connection string created manage //the contents of connection string. var sqlconnection = new sqlconnectionstringbuilder(); sqlconnection.datasource = "192.168.10.3"; sqlconnection.userid = "gp"; sqlconnection.password = "gp"; sqlconnection.initialcatalog = convert.tostring(cmbdatabases.selectedvalue); string connectionstring = sqlconnection.connectionstring; sqlconnection sconnection = new sqlconnection(connectionstring); //to open connection. sconnection.open(); //query select table_names have primary_keys. string selectprimarykeys = @" select table_name information_schema.table_constraints constraint_type = 'primary key' , table_name <> 'dtproperties' order table_name"; //create command object sqlcommand scommand = new sqlcommand(selectprimarykeys, sconnection); try { //create dataset dataset dslistofprimarykeys = new dataset("information_schema.table_constraints"); //create dataadapter object sqldataadapter sdataadapter = new sqldataadapter(selectprimarykeys, sconnection); //provides master mapping between sourcr table //and system.data.datatable sdataadapter.tablemappings.add("table", "information_schema.table_constraints"); //fill dataset sdataadapter.fill(dslistofprimarykeys); //bind result combobox primary key tables dataviewmanager dvmlistofprimarykeys = dslistofprimarykeys.defaultviewmanager; dgresultview.datasource = dslistofprimarykeys .tables["information_schema.table_constraints"]; } catch(exception ex) { //all exceptions handled , written in eventlog. eventlog log = new eventlog("application"); log.source = "mfdbanalyser"; log.writeentry(ex.message); } { //if connection not closed close connection if(sconnection.state != connectionstate.closed) { sconnection.dispose(); } } }
can me in removing these hardcoded things , taking local server address, userid , password app.config file directly???
sure.
first, open app.config or web.config file.
look following section
<appsettings> <add key="...." value="....." /> .... </appsettings>
if doesn't exist, should added.
now add following key/values...
<add key="myserver" value="192.168.10.3" /> <add key="myuserid" value="gp" /> <add key="mypassword" value="gp" />
and example of app.config can like...
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appsettings> <add key="myserver" value="192.168.10.3" /> <add key="myuserid" value="gp" /> <add key="mypassword" value="gp" /> </appsettings> </configuration>
kewl. lets update code. trick use configurationmanager.appsettings class :-
change...
sqlconnection.datasource = "192.168.10.3"; sqlconnection.userid = "gp"; sqlconnection.password = "gp";
to..
sqlconnection.datasource = configuationmanager.appsettings["myserver"]; sqlconnection.userid = configuationmanager.appsettings["myuserid"]; sqlconnection.password = configuationmanager.appsettings["mypassword"];
now can change values of key/values (in app.config or web.config) without having compile code :)
hth
Comments
Post a Comment