sqlcloseconnection.aspx

来自「asp.net技术内幕的书配源码」· ASPX 代码 · 共 45 行

ASPX
45
字号
<%@ Page Language="C#"%>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat=server>

SqlConnection conPubs;


void Page_Load(Object sender , EventArgs e) 
{
	SqlDataReader dtrAuthors;

	dtrAuthors = GetAuthors();
	while (dtrAuthors.Read())
	{
		lblAuthors.Text += "<li>";
		lblAuthors.Text += dtrAuthors[ "au_lname" ];
	}
	dtrAuthors.Close();
	lblAuthors.Text += "<hr> Connection is:" + conPubs.State.ToString();
}  

SqlDataReader GetAuthors() 
{
	SqlCommand cmdAuthors;

	conPubs = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;database=pubs" );
	cmdAuthors = new SqlCommand( "select * from Authors", conPubs );
	conPubs.Open();
	return cmdAuthors.ExecuteReader( CommandBehavior.CloseConnection );
}

</Script>

<html>
<head><title>SqlCloseConnection.aspx</title></head>
<body>

<asp:Label
  id="lblAuthors"
  Runat="Server" />

</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?