Configure the database connection string in WebConfig. The Code is as follows:
Copy codeThe Code is as follows: <connectionStrings>
<Add name = "ConnectionString" connectionString = "user id = username; password = password; initial catalog = database name; data source = server name"/>
</ConnectionStrings>
Then obtain the connection string in Webform_1.aspx.cs and add the following reference;Copy codeThe Code is as follows: using System. Configuration;
Using System. Data;
Using System. Data. SqlClient;
Code:Copy codeThe Code is as follows: SqlConnection con;
Protected void Page_Load (object sender, EventArgs e)
{
ConnectDB ();
}
Private void ConnectDB ()
{
String ConString = ConfigurationManager. ConnectionStrings ["ConnectionString"]. ConnectionString;
Con = new SqlConnection (ConString );
Con. Open ();
SqlCommand com = new SqlCommand ();
SqlDataReader sdr;
String sqlstr = "select * from item ";
Com. CommandText = sqlstr;
Com. Connection = con;
Sdr = com. ExecuteReader ();
While (sdr. Read ())
{
Response. Write (sdr ["field name"]. ToString () + "</br> ");
}
Sdr. Close ();
Sdr = null;
}
This is enough. It's very simple. I hope my friends will give me more advice and appreciate it!