简单获取一个表的结构

 1<%@ Page Language="C#"%>
 2<%@ Import Namespace="System.Data" %>
 3<%@ Import Namespace="System.Data.SqlClient" %>
 4
 5<script runat=server>
 6
 7void Page_Load(Object sender , EventArgs e) 
 8{
 9    SqlConnection conn;
10    SqlCommand cm;
11    SqlDataReader dr;
12    DataTable dt;
13
14    conn = new SqlConnection( 
15"Server=localhost;Integrated Security=SSPI;database=Bank" 
16);
17    cm = new SqlCommand( "Select * from 
18table", conn );
19    conn.Open();
20    dr = cm.ExecuteReader( CommandBehavior.KeyInfo | CommandBehavior.SchemaOnly );
21    dt = dr.GetSchemaTable();
22    dg.DataSource = dt;
23    dg.DataBind();
24    dr.Close();
25    
26    conn.Close();
27}

28
29</Script>
30
31<html>
32<head><title>SqlGetSchemaTable.aspx</title></head>
33<body>
34
35<asp:DataGrid
36  id="dg"
37  CellPadding="4"
38  HeaderStyle-BackColor="lightgreen"
39  Runat="Server" />
40
41</body>
42</html>
posted @ 2006-06-25 11:20  wenanry  阅读(355)  评论(0编辑  收藏  举报