Default. aspx页面装入时执行的事件过程代码如下 protected void Page Load(object sender, EventArgs e) string consTr= ConfigurationManager Connection Strings["StudentDBConnectionString"]. ToStringO SqlConnection sqlconn=new SqlConnection(consTr); Labell Text =connStr +"<br/> LabellText= Labell Text + sqlconn State ToString(+<br Iconn open(; Labell Text=Labell Text sqlconn State ToString(+"<br A sqIconn. Close sqlconn=null (2)使用 SqlCommand对象的 ExecuteScalarO方法来返回 Student Info表中记录的数目( SELECT语句使用 COUNT聚合 函数返回指定表中的行数的单个值) protected void Page Load(object sender, EventArgs e) string connString Configuration Manager. Connection Strings["Student D BConnectionString"].ToString(; Int32 count=0;∥1计学生人数 using( SqlConnection conn= new SqlConnection( connString)∥创建 Connection对象conn CM2 Cring sqlstr="SELECT COUN+) FROM StudentInfo:OL字符串 nd ordersCMd=new SqlCommand(sqlstr, conn) conn. Open count=(lnt32) orders CMD. Execute Scalar,∥将返回的记录数强制转换成整型 catch( Exception ex)/捕捉错误 Response. Write( ex Message),∥显示出错信息 Response. Write("学生总数为”+ count. aStringe+"人!"),∥,示“学生总数为XX人!”Default.aspx 页面装入时执行的事件过程代码如下: protected void Page_Load(object sender, EventArgs e) { string connStr = ConfigurationManager.ConnectionStrings["StudentDBConnectionString"].ToString(); SqlConnection sqlconn = new SqlConnection(connStr); Label1.Text = connStr + "<br />"; Label1.Text = Label1.Text + sqlconn.State.ToString() + "<br />"; sqlconn.Open(); Label1.Text = Label1.Text + sqlconn.State.ToString() + "<br />"; sqlcommand = null; sqlconn.Close(); sqlconn = null; } (2) 使 用 SqlCommand 对 象 的 ExecuteScalar() 方法来返回 StudentInfo 表中记录的数目(SELECT 语句使用 COUNT 聚合 函数返回指定表中的行数的单个值) protected void Page_Load(object sender, EventArgs e) { string connString = ConfigurationManager.ConnectionStrings["StudentDBConnectionString"].ToString(); Int32 count = 0; //统计学生人数 using (SqlConnection conn = new SqlConnection(connString)) //创建 Connection 对象 conn { string sqlstr = "SELECT COUNT(*) FROM StudentInfo"; //SQL 字符串 SqlCommand ordersCMD = new SqlCommand(sqlstr, conn); try { conn.Open(); count = (Int32)ordersCMD.ExecuteScalar(); //将返回的记录数强制转换成整型 } catch (Exception ex) //捕捉错误 { Response.Write(ex.Message); //显示出错信息 } } Response.Write("学生总数为" + count.ToString() + "人!"); //显示“学生总数为 XX 人!” }