如何从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>
我也没有得到任何输出错误 如何解决这一个???
答案 0 :(得分: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
此外,我建议使用代码隐藏文件并将代码放在那里,因为它更清晰。
答案 1 :(得分: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