In general, we will configure the relevant connection string in the web. config file, which is recorded here for future use.
1. Commonly Used <connectionstrings> nodes
A: SQL Server Authentication connection string
View code
1 <connectionstrings>
2 <Add name = "connectionstringname" connectionstring = "Server =.; database = database name; uid = username; Pwd = password ;"
3 providername = "system. Data. sqlclient"> </Add>
4 </connectionstrings>
B. Windows Authentication connection string
View code
1 <connectionstrings>
2 <Add name = "connectionstringname" connectionstring = "Data Source = server name; initial catalog = database name; Integrated Security = sspi ;"
3 providername = "system. Data. sqlclient"> </Add>
4 </connectionstrings>
In addition, you can use <connectionstrings configsource = "configration \ connectionstring. config"/> to specify the definition in an external file.
Code: see the above.
Method for obtaining connectionstring: system. configuration. configurationmanager. connectionstrings ["connectionstringname"]. connectionstring;
2. Configure the node in <etettings> (for Windows verification, refer to 1. B)
View code
1 <deleetask>
2 <add key = "connectionstringname" value = "Server =.; database = database name; uid = username; Pwd = password;"/>
3 </appsettings>
Method for obtaining connectionstring: system. configuration. configurationmanager. deleetpipeline ["connectionstringname"]. tostring ();