asp.net mvc - How to set Password and Username in web.config -
in mvc application use connection string set in web.config via
private connectionstring string = configurationmanager.connectionstrings("dbcs").connectionstring
and have no problems db connection. since need password , username log database hardcoded in web.config
<connectionstrings> <add name="dbcs" connectionstring="server=win\sqlexpress;database=mydb; uid=myusername;password=mypassword" providername="system.data.sqlclient" /> </connectionstrings>
i looking way send password , username userinterface config.web file first off thought configurationmanager class should provide property cannot find something. can explain me how this?
you can save value in app settings:
<appsettings> <add key="dbcs" value="data source=win\sqlexpress;initial catalog=mydb;user id={0};password={1}" /> </appsettings>
and following:
using system.data.sqlclient; public void dodatabaseoperations(string _username, string _password) { string connetionstring = null; sqlconnection cnn ; connetionstring = string.format(configurationmanager.appsettings("dbcs"), _username, _password); cnn = new sqlconnection(connetionstring); try { cnn.open(); // code here cnn.close(); } catch (exception ex) { // handle exception } }
vb.net equivalent:
imports system.data.sqlclient public sub dodatabaseoperations(_username string, _password string) dim connetionstring string = nothing dim cnn sqlconnection connetionstring = string.format(configurationmanager.appsettings("dbcs"), _username, _password) cnn = new sqlconnection(connetionstring) try cnn.open() ' code here cnn.close() ' handle exception catch ex exception end try end sub
Comments
Post a Comment