specify Asp.net LocalSqlServer connection string in code -
normally, define connection string in web.config
is there way can specify connection string in code, in application_start?
that connection string used connect aspnetdb example:
if you're not using entity framework, can use sqlconnectionstringbuilder class.
example msdn:
sub main() ' create new dbconnctionstringbuilder, , add items ' internal collection of key/value pairs. dim builder new dbconnectionstringbuilder() builder.add("data source", "c:\mydata\mydb.mdb") builder.add("provider", "microsoft.jet.oledb.4.0") builder.add("jet oledb:database password", "*******") builder.add("jet oledb:system database", _ "c:\mydata\workgroup.mdb") ' set row-level locking. builder.add("jet oledb:database locking mode", 1) ' display contents of connection string, ' contain key/value pairs delimited ' semicolons. console.writeline(builder.connectionstring) console.writeline() ' clear dbconnectionstringbuilder, , assign complete ' connection string it, demonstrate how ' class parses connection strings. builder.clear() builder.connectionstring = _ "data source=(local);initial catalog=adventureworks;" & _ "integrated security=sspi" ' dbconnectionstringbuilder class has parsed contents, ' can work individual key/value pair. builder("data source") = "." console.writeline(builder.connectionstring) console.writeline() ' because dbconnectionstringbuilder class doesn't ' validate key/value pairs, can use class ' store semicolon-delimited list. following ' snippet places arbitrary string connectionstring ' property, changes 1 of values, , displays ' resulting string. builder.clear() builder.connectionstring = _ "value1=10;value2=20;value3=30;value4=40" builder("value2") = 25 console.writeline(builder.connectionstring) console.writeline() builder.clear() try ' assigning invalid connection string ' throws argumentexception. builder.connectionstring = "xxx" catch ex argumentexception console.writeline("invalid connection string.") end try console.writeline() console.writeline("press enter finish.") console.readline() end sub
Comments
Post a Comment