关于ADO.NET的一个小问题,高手帮忙进来解释下
以下有两个方式但是结果都相同,请高手帮忙解释下,两种方式有什么差别
方式1using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//判断页面是否初次加载
if(!Page.IsPostBack)
{
//创建连接字符串
string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
//创建连接对象
SqlConnection conn = new SqlConnection(connStr);
//打开数据库
conn.Open();
//创建执行对象
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from studentinfo";
SqlDataReader dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
if(conn.State==ConnectionState.Open)
{
conn.Close();
}
}
}
}
方式2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//判断页面是否初次加载
if(!Page.IsPostBack)