
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<HTML>
	<HEAD>
		<title>获得数据库中表的数目和名称</title>
		<script language=C# runat=server>
		
			void Page_Load(object sender, System.EventArgs e)
			{
				string ConnStr = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
				string listQuery = "SELECT name FROM sysobjects WHERE xtype = 'U'";
				string sumQuery = "SELECT COUNT(*) FROM sysobjects WHERE xtype = 'U'";

				SqlCommand myCommand = new SqlCommand();
				myCommand.Connection = new SqlConnection(ConnStr);

				myCommand.Connection.Open();

				// 获得用户表的数目
				myCommand.CommandText = sumQuery;
				SumLabel.Text = myCommand.ExecuteScalar().ToString();

				// 获得用户表的列表
				myCommand.CommandText = listQuery;
				SqlDataReader myReader = myCommand.ExecuteReader();
				ListLabel.Text = "";
				while(myReader.Read())
				{
					ListLabel.Text += "<br>&nbsp;&nbsp;" + myReader[0].ToString();
				}
				myReader.Close();
			
				myCommand.Connection.Close();			
			}
		
		</script>
	</HEAD>
	<body>
		<form id="Form1" method="post" runat="server">
			<h3>获得数据库中表的数目和名称</h3>
			用户表的数目：
			<asp:Label id="SumLabel" runat="server"></asp:Label><br>
			<br>
			用户表的列表：
			<asp:Label id="ListLabel" runat="server"></asp:Label>
		</form>
	</body>
</HTML>
