2013-08-02 3 views
2

vb.net의 데이터베이스 테이블에서 데이터를 검색하는 방법. 나는 단지 빈 페이지 를 만들어 출력을하지 않는 노력 그리고 내 코드는 다음과 같음 :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> 

그리고 내가 어떻게이 일을 해결하는 방법도 을 모든 출력 오류 중 하나를 얻을하지 않습니다 ???

답변

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
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 
관련 문제