2

如何从 vb.net 中的数据库表中检索数据。当我尝试时,我没有得到任何输出它只会创建一个空白页我的代码是:

<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script runat="server">
sub Page_Load()
Dim con As New SqlConnection
Dim cmd As New SqlCommand

con.ConnectionString = ///my connection string///
con.Open()
cmd.Connection = con
cmd.CommandText = "select * from det"
Dim lrd As SqlDataReader = cmd.ExecuteReader()
End sub         

</script>
<form runat="server">
<asp:Repeater id="customers" runat="server">

<HeaderTemplate>
<table border="1" width="100%">
<tr bgcolor="#b0c4de">
<th>ID</th>
<th>Name</th>
<th>Address</th>
<th>Age</th>
<th>Gender</th>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr bgcolor="#f0f0f0">
<td><%#DataBinder.Eval(Container.DataItem, "id")%> </td>
<td><%#DataBinder.Eval(Container.DataItem, "name")%> </td>
<td><%#DataBinder.Eval(Container.DataItem, "address")%> </td>
<td><%#DataBinder.Eval(Container.DataItem, "age")%> </td>
<td><%#DataBinder.Eval(Container.DataItem, "gender")%> </td>

</tr>
</ItemTemplate>

<FooterTemplate>
</table>
</FooterTemplate>

</asp:Repeater>
</form>

而且我也没有得到任何输出错误如何解决这个错误???

4

2 回答 2

2

您正在检索数据,但没有对它做任何事情:

sub Page_Load()

   Dim con As New SqlConnection
   Dim cmd As New SqlCommand

   con.ConnectionString = ///my connection string///
   con.Open()
   cmd.Connection = con
   cmd.CommandText = "select * from det"

   customers.DataSource = cmd.ExecuteReader() //here we bind data to repeater.
   customers.DataBind();

End sub         

另外我建议使用文件后面的代码并将您的代码放在那里,因为它更清晰。

于 2013-08-02T06:02:40.903 回答
1
Public Sub OnPageLoad()
        Dim con As New SqlConnection
        Dim cmd As New SqlCommand

        con.ConnectionString = String
        cmd.Connection = con
        cmd.CommandText = "SELECT * FROM TABLE_NAME"
        con.Open()
        customers.DataSource = cmd.ExecuteNonQuery()
        customers.DataBind()
        con.Close()
End Sub
于 2013-08-02T07:06:52.430 回答