<%@ Import Namespace="System.Data.SqlClient" %>
<HTML>
	<HEAD>
		<title>将 DataReader 做为数据源绑定到 DataGrid 控件</title>
		<script language="C#" runat="server">
		
			SqlDataReader dr;
			
			void Page_Load(object sender, System.EventArgs e)
			{
				// 数据连接字符串及 SQL 语句
				string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
				string query = "SELECT * FROM Categories";

				// 创建并打开数据库连接
				SqlCommand myCommand = new SqlCommand(query, new SqlConnection(ConnStr));
				myCommand.Connection.Open();
				
				// 执行数据库操作并返回 DataReader 对象
				dr = myCommand.ExecuteReader();

				// 将 DataReader 做为数据源绑定到 DataGrid
				DataGrid1.DataSource = dr;
				DataGrid1.DataBind();

				// 关闭 DataReader 对象和数据库连接
				dr.Close();
				myCommand.Connection.Close();
			}
	
		</script>
	</HEAD>
	<body>
		<form id="Form1" method="post" runat="server">
			<h3>将 DataReader 做为数据源绑定到 DataGrid 控件</h3>
			<br>
			<asp:datagrid id="DataGrid1" runat="server"></asp:datagrid>
		</form>
	</body>
</HTML>
