<%@ Register TagPrefix="Title" TagName="TitleShow" Src="/aspnetdata/Title.ascx" %>
<%@ Register TagPrefix="AllButton" TagName="Butn" Src="btn.ascx" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web" %>
<html>
<head>
	<script language="VB" runat="server">
	Dim Source,Source1 as SqlConnection
	sub Page_load(sender as object, e as EventArgs)
		AllTables.ToolTip="AllTables"
		Source = Ctype(Session("NowSTKSQL"),SqlConnection)
		Source1 = Ctype(Session("NowMaster"),SqlConnection)
		if IsPostBack=false then
			Dim DS as DataSet = new DataSet()
			Dim NComm as SqlDataAdapter = new SqlDataAdapter("USE stockSQL EXEC dbo.sp_tables", Source1)
			NComm.Fill(DS,"AllTable")
			DS.Tables("AllTable").DefaultView.RowFilter = "TABLE_TYPE NOT LIKE 'SYSTEM TABLE'"
			AllTables.DataSource=DS.Tables("AllTable").DefaultView
			AllTables.DataValueField="TABLE_NAME"'将数据表名称置于DropDownlist
			AllTables.DataBind()
		end if
	end sub
	sub selectTable(sender as object, e as EventArgs)
		Dim NComm as SqlDataAdapter = new SqlDataAdapter("select * from " & AllTables.SelectedItem.Text, Source)
		Dim DS as DataSet = new DataSet()
		Dim AllFields as ArrayList = new ArrayList()
		NComm.Fill(DS,AllTables.SelectedItem.Text)
		dim Col as DataColumn
		for each Col in DS.Tables(AllTables.SelectedItem.Text).Columns
			AllFields.Add (Col.ColumnName)'加入列名
		NEXT
		SourceFd.DataSource=AllFields
		SourceFd.DataBind()
		TargetFd.DataSource=""'清除目的ListBox的项目
		TargetFd.DataBind()
	END SUB
	sub GetRecord(sender as object, e as EventArgs)
		Cus.Visible=false
		Dim QS as string=""
		Dim CItem as ListItem
		for each CItem in TargetFd.Items
			QS+= "," & CItem.Text
		next
		if QS="" then 
			nn.Text="未选择列"
			exit sub
		else
			QS=QS.Remove(0,1)
		end if
		QS="select " & QS & " From " & AllTables.SelectedItem.Text
		Dim DS as DataSet = new DataSet()
		Dim NComm as SqlDataAdapter = new SqlDataAdapter(QS, Source)
		NComm.Fill(DS, AllTables.SelectedItem.Text)
		Cus.DataSource=DS.Tables(AllTables.SelectedItem.Text).DefaultView
		Cus.DataBind()
		Cus.Visible=true
	end sub
	</script>
</head>
<body background="/aspnetdata/images/winback.gif">
<Title:TitleShow id="BookT" Runat="server" />
<form runat="server">
查看 <asp:hyperlink id="h1" NavigateUrl="/aspnetdata/shows.aspx?cate=c&file=ch12/12-2/vb/btn.ascx" Text="btn.ascx" runat="server" target="_blank"/><br>
<table align="center"><tr><td align="left" colspan="3">选择数据表：<asp:dropdownlist id="AllTables" Autopostback="True" OnSelectedIndexChanged="selectTable" Runat="server" /></td></tr>
<tr><td><asp:ListBox Runat="server" ID="SourceFd" SelectionMode="Multiple" Width="140" Rows="8" /></td>
<td><AllButton:Butn runat="server" id="ChoiceField" SourceList="SourceFd" TargetList="TargetFd" /></td>
<td><asp:ListBox Runat="server" ID="TargetFd" Width="140" Rows="8" /></td></tr>
<tr><td align="center" colspan="3"><asp:button id="b1" OnClick="GetRecord" Text="确定" Runat="server" /><br><asp:label id="nn" runat="server" /></td></tr>
</table>
<hr>
      <ASP:DataGrid id="Cus" runat="server"
        BorderColor="black"
        BorderWidth="1"
        GridLines="Both"
        CellPadding="3"
        CellSpacing="0"
        Font-Name="Verdana"
        Font-Size="8pt"
        HeaderStyle-BackColor="#aaaadd"
AutoGenerateColumns="True"
>
</ASP:DataGrid>

</form>
</body>