Web. config database connection, web. config
Method 1: connectionsStrings
First configure the web. config file
<Deployments>
<ConnectionStrings>
<Add name = "Test" connectionString = "server =.; database = Testbase; uid = sa; pwd = asdf123 _;">
</ConectionStrings>
<Deployments>
The server indicates the database address, the local database can be represented by "." or "localhost", the database indicates the database name, the uid indicates the name of the database to log on to, and the pwd indicates the password to log on to the database;
The aspx page is simpler.
First reference the namespace using System. Web. Configuration;
String con = ConfigurationManager. ConectionStrings ["Test"]. ConnectionString;
If (! String. IsNullOrEmpty (con ))
{
TextBox1.Text = "connected to the database! ";
}
Else
{
TextBox1.Text = "failed to connect to the database! ";
}
Method 2: appSettings
Web. config Configuration
<Deployments>
<Deleetask>
<Add key = "Test" value = "server =.; database = Testbase; uid = sa; pwd = asdf123 _;/>
</AppSettings>
</Deployments>
The server indicates the database address, the local database can be represented by "." or "localhost", the database indicates the database name, the uid indicates the name of the database to log on to, and the pwd indicates the password to log on to the database;
Aspx page
First, the application namespace first references the namespace using System. Web. Configuration;
String con = configurationManager. configuretting ["Test"];
If (! String. IsNullOrEmpty (con ))
{
TextBox1.Text = "connected to the database! ";
}
Else
{
TextBox1.Text = "failed to connect to the database! ";
}
1. Integrated Windows Authentication syntax example
String con = "server =.; database = myschool; integrated security = SSPI ";
2. The Windows Authentication Mode in SQL Server 2005 is as follows:
String con = "server =.; database = school; uid = sa; pwd = asdf123 _";
3. the SQL Server Authentication Mode in SQL Server 2005 is as follows:
String con = "data source =.; initial catalog = school; user id = sa; pwd = asdf123 _";