1. get the table list 2. get data for each table 3. bind the data to a datagridhere is a simple demo:<%@ Page Language="C#" %> <%@ Import Namespace="System.Collections" %> <%@ Import Namespace="System.Data.SqlClient" %> <script runat="server"> void Page_Load(Object sender, EventArgs e) { String sConn = "Server=localhost;Database=pubs;UID=sa;PWD=;"; SqlConnection conn = new SqlConnection(sConn); SqlCommand com = new SqlCommand("select name from sysobjects where type='U'", conn); conn.Open(); SqlDataReader dr = com.ExecuteReader(); ArrayList tableList = new ArrayList(); while (dr.Read()) tableList.Add(dr.GetString(0)); dr.Close(); foreach (String sName in tableList) { Literal li = new Literal(); li.Text = sName + "<BR>"; this.Controls.Add(li); com = new SqlCommand("select * from " + sName, conn); dr = com.ExecuteReader(); DataGrid dg = new DataGrid(); this.Controls.Add(dg); dg.DataSource = dr; dg.DataBind(); dr.Close(); } conn.Close(); } </script>
to spring_ok(spring.z) : jion在哪里用??怎样用?为什么你的例子中看不到?
我的例子是显示一个表的。想同时显示多个表,就将SQL语句修改: 比如: SqlCommand myCommand = new SqlCommand("select * from Authors", myConnection); 改成: SqlCommand myCommand = new SqlCommand("select Authors.*, Titles.* from Authors inner join Titles on Authors.au_id = Titles.au_id", myConnection);
<%@ Import Namespace="System.Data.SqlClient" %><html>
<script language="C#" runat="server"> protected void Page_Load(Object sender, EventArgs e) {
SqlConnection myConnection = new SqlConnection("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes");
SqlCommand myCommand = new SqlCommand("select * from Authors", myConnection); myConnection.Open(); SqlDataReader dr = myCommand.ExecuteReader(); MyDataGrid.DataSource = dr;
MyDataGrid.DataBind(); myConnection.Close();
}</script><body> <h3><font face="Verdana">Simple Select to a DataGrid Control</font></h3> <ASP:DataGrid id="MyDataGrid" runat="server"
Width="700"
BackColor="#ccccff"
BorderColor="black"
ShowFooter="false"
CellPadding=3
CellSpacing="0"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="false"
/></body>
</html>
2. get data for each table
3. bind the data to a datagridhere is a simple demo:<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
String sConn = "Server=localhost;Database=pubs;UID=sa;PWD=;";
SqlConnection conn = new SqlConnection(sConn);
SqlCommand com = new SqlCommand("select name from sysobjects where type='U'", conn);
conn.Open();
SqlDataReader dr = com.ExecuteReader();
ArrayList tableList = new ArrayList();
while (dr.Read())
tableList.Add(dr.GetString(0));
dr.Close();
foreach (String sName in tableList)
{
Literal li = new Literal();
li.Text = sName + "<BR>";
this.Controls.Add(li);
com = new SqlCommand("select * from " + sName, conn);
dr = com.ExecuteReader();
DataGrid dg = new DataGrid();
this.Controls.Add(dg);
dg.DataSource = dr;
dg.DataBind();
dr.Close();
} conn.Close();
}
</script>
jion在哪里用??怎样用?为什么你的例子中看不到?
比如:
SqlCommand myCommand = new SqlCommand("select * from Authors", myConnection);
改成:
SqlCommand myCommand = new SqlCommand("select Authors.*, Titles.* from Authors inner join Titles on Authors.au_id = Titles.au_id", myConnection);