1. Add a connection to the <connectionStrings> tab
<connectionStrings>
<add name="ConnectionName" connectionstring= "server=.\sqlexpress;database=databasename; Userid=sa; Password=abc123 "
Providername="System.Data.SqlClient"/>
</connectionStrings>
Or
<connectionStrings>
<add name="ConnectionName" connectionstring= "server=.\sqlexpress; attachdbfilename=| Datadirectory|\\data.mdf; Integrated security=true "providername=" System.Data.SqlClient "/>
</connectionStrings>
Get the connection string in the <connectionStrings> tag in the program code:
Reference namespaces:
- Using System.Configuration;
- String connstr = configurationmanager.connectionstrings["ConnectionName"]. ToString ();
2. Add a connection to the <appSettings> tab
<appSettings>
<add key="ConnectionName" value= "server=.\sqlexpress;database=databasename; User Id=sa; Password=abc123 "/>
</appSettings>
Or
<appSettings>
<add key="ConnectionName" value= "server=.\sqlexpress; attachdbfilename=| datadirectory|\\data.mdf;integrated security=true "/>
</appSettings>
Get the connection string in the <appSettings> tag in the program code:
Reference namespaces:
- Using System.Configuration;
- String connstr = configurationmanager.appsettings["ConnectionName"]. ToString ();
Configure the database connection in the Web. config file in the ASP. NET project and get the connection string in the program code