本文是介绍如何使用SqlDataSource进行数据库记录查询 <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:SqlDataSource ID="sds" runat="server" DataSourceMode="DataReader" SelectCommand="select * from Employees where LastName=@LastName and FirstName=@FirstName " ConnectionString="server=.;database=Northwind;uid=xxx;pwd=xxx"> <SelectParameters> <asp:ControlParameter ControlID="txtLastName" DbType="String" Name="LastName" /> <asp:Parameter DbType="String" Name="FirstName" /> </SelectParameters> </asp:SqlDataSource> <div> LastName:<asp:TextBox ID="txtLastName" Text="Davolio" runat="server"></asp:TextBox> </div> <div> FirstName:<asp:TextBox ID="txtFirstName" Text="Nancy" runat="server"></asp:TextBox> </div> <asp:Button ID="btn" runat="server" Text="Query" /> <div style="height:20px"></div> <asp:PlaceHolder ID="ph" Visible="false" runat="server"> <div> <fieldset> <legend>Person Information</legend> <br /> Title:<asp:TextBox ID="txtTitle" runat="server"></asp:TextBox> BirthDate:<asp:TextBox ID="txtBirthDate" runat="server"></asp:TextBox> Address:<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox> </fieldset> </div> </asp:PlaceHolder> </form> </body> </html> Imports System.Data Imports System.Web.UI Imports System.Web.UI.WebControls Partial Class VB_Default Inherits System.Web.UI.Page Protected Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click 'sds.SelectParameters("LastName").DefaultValue = "Davolio" sds.SelectParameters("FirstName").DefaultValue = txtFirstName.Text ' "Nancy" Dim dt As DataTable = GetDataTable(sds.DataSourceMode, sds.Select(System.Web.UI.DataSourceSelectArguments.Empty)) If dt IsNot Nothing And dt.Rows.Count > 0 Then ph.Visible = True txtTitle.Text = dt.Rows(0)("Title").ToString() txtBirthDate.Text = dt.Rows(0)("BirthDate").ToString() txtAddress.Text = dt.Rows(0)("Address").ToString() Else ph.Visible = False End If End Sub End Class