Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Data. SqlClient;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
// ------------------- SqlConnection, SqlCommand, and SqldataReader usage -------------------//
// The following four forms are equivalent. If your database is connected to Windows, change the connection string to "server =.; database = northwind; integrated security = true ;"
// If your database is an instance name, simply change "server =." to "server = your computer name Instance name ".
SqlConnection con = new SqlConnection ();
Con. ConnectionString = "server =.; database = northwind; uid = sa; pwd =;"; // remove the last semicolon in double quotation marks
Con. Open ();
SqlCommand cmd = con. CreateCommand ();
Cmd. CommandText = "select * from MERs ";
SqlDataReader sdr = cmd. ExecuteReader ();
This. GridView1.DataSource = sdr;
This. GridView1.DataBind ();
Sdr. Close ();
Con. Close ();
// SqlConnection con = new SqlConnection ();
// Con. ConnectionString = "server =.; database = northwind; uid = sa; pwd =;"; // remove the last semicolon in double quotation marks
// Con. Open ();
// SqlCommand cmd = new SqlCommand ("select * from MERs ");
// Cmd. Connection = con;
// SqlDataReader sdr = cmd. ExecuteReader ();
// This. GridView1.DataSource = sdr;
// This. GridView1.DataBind ();
// Sdr. Close ();
// Con. Close ();
/// I use this method most often, and the connection object is the public object of the entire program. Therefore, I usually encapsulate the database connection into a class, in this way, you can call it anywhere in the program at any time.
// SqlConnection con = new SqlConnection ("server =.; database = northwind; uid = sa; pwd =;"); // remove the last semicolon in double quotation marks
// Con. Open ();
// SqlCommand cmd = new SqlCommand ("select * from MERs", con );
// SqlDataReader sdr = cmd. ExecuteReader ();
// This. GridView1.DataSource = sdr;
// This. GridView1.DataBind ();
// Sdr. Close ();
// Con. Close ();
// SqlConnection con = new SqlConnection ();
// Con. ConnectionString = "server =.; database = northwind; uid = sa; pwd =;"; // remove the last semicolon in double quotation marks
// Con. Open ();
// SqlCommand cmd = new SqlCommand ();
// Cmd. Connection = con;
// Cmd. CommandText = "select * from MERs ";
// Cmd. CommandType = CommandType. Text; // This statement is redundant, because the default value is Text.
// SqlDataReader sdr = cmd. ExecuteReader ();
// This. GridView1.DataSource = sdr;
// This. GridView1.DataBind ();
// Sdr. Close ();
// Con. Close ();
}