<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data" %>
<HTML>
<script language="VB" runat="server">
Dim MyConnection As SqlConnection
Sub Page_Load(Sender As Object, E As EventArgs)
Dim MyConnectString As String = _
"data source=wxj;" & _
"initial catalog=pubs; " & _
"user id=sa; " & _
"pwd=password;" & _
"Trusted_Connection=yes"
MyConnection = New SqlConnection(myConnectString)
'创建连接
If Not (IsPostBack)
'为下拉列表框添加列表项
Dim DS As DataSet
Dim MyCommand As SqlDataAdapter
MyCommand = New SqlDataAdapter("select distinct State from Authors", MyConnection)
DS = new DataSet()
MyCommand.Fill(DS, "States")
MySelect.DataSource= DS.Tables("States").DefaultView
MySelect.DataBind()
End If
End Sub
Sub GetAuthors_Click(Sender As Object, E As EventArgs)
Dim MyDs As DataSet
Dim MyCommand As SqlDataAdapter
Dim SelectCommand As String = "select * from Authors where state = @State"
MyCommand = New SqlDataAdapter(SelectCommand, MyConnection)
'创建命令
MyCommand.SelectCommand.Parameters.Add(New SqlParameter("@State", SqlDbType.NVarChar, 2))
MyCommand.SelectCommand.Parameters("@State").Value = MySelect.Value
'添加并设值参数
MyDs = new DataSet()
MyCommand.Fill(MyDs, "作者")
MyDataGrid.DataSource=MyDs.Tables("作者").DefaultView
MyDataGrid.DataBind()
End Sub
</script>
<body style="FONT: 10.5pt 宋体">
<form runat="server" ID="Form1">
<h3><font face="宋体">动态参数查询</font></h3>
<P>
选择州:
<select id="MySelect" DataTextField="State" runat="server" NAME="MySelect">
</select>
<asp:Button id="Button1" runat="server" OnClick="GetAuthors_Click" Text="获取作者"></asp:Button></P>
<p>
<ASP:DataGrid id="MyDataGrid" runat="server" BackColor="White" BorderColor="#CC9966" CellPadding="4" HeaderStyle-BackColor="#aaaadd" EnableViewState="False"></ASP:DataGrid>
</p>
</form>
</body>
</HTML>