gridview显示数据的问题
Imports System.Data
Imports System.Data.Odbc
Partial Class dataview
Inherits System.Web.UI.Page
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
Dim myConnection As New OdbcConnection
Dim DB As New qulity1
myConnection = DB.odbccreate() //数据库连接(odbc+db2) 连接没有问题
Dim sql As String = "select * from tuser "
Dim cmd = New OdbcCommand(sql, myConnection)
Dim rd As OdbcDataReader '声明一个datareader对象
Dim mydatatable As New DataTable
myConnection.Open()
rd = cmd.executereader() '执行SQL指令,并将其结果设置给datareader
mydatatable.Load(rd)
If (mydatatable.Rows).Count > 0 Then
GridView1.DataSource = mydatatable
GridView1.DataBind()
Else
Label1.Text = "Unable to connect to the database. "
End If
End Sub
End Class
页面显示空白,表中有数据,连接的串没有问题,请各位帮忙看看为什么调不出数据呢?
------解决方案--------------------If (mydatatable.Rows).Count > 0 Then
-------------------
if mydatatable.Rows.Count> 0
------解决方案--------------------可能是你的OdbcDataReader没有工作吧?怎么没发现while(rd.read)语句?不清楚好久没用reader了。
几个问题:
1.PageLoad中定义的myConnection居然没有关闭,不好;
2.(mydatatable.Rows).Count ==0 为数据库是否连接上的标志,是否过于牵强?用try..catch?