使用vb从asp.net中的数据库中检索数据

时间:2014-03-04 09:19:23

标签: vb.net

下面是将数据提取到文本框中的代码,但是它无法正常显示错误没有数据退出行/列,而数据和数据字段完全没问题。 请帮忙。

    Dim Connection As OleDbConnection
    Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/db/QardanHasana.mdb"))
    Connection.Open()

    Dim ejamaatregcmd As OleDbCommand
    Dim ejamaatregdtrdr As OleDbDataReader
    ejamaatregcmd = New OleDbCommand("SELECT ITSData.[EJamaatID], ITSData.[ITSFirstName] FROM ITSData WHERE  EjamaatID= @EjamaatID", Connection)
    ejamaatregcmd.Parameters.Add(New OleDbParameter("@EjamaatID", txtEjamaatID.Text))
    ejamaatregdtrdr = ejamaatregcmd.ExecuteReader()


    If ejamaatregdtrdr.HasRows Then
        txtFirstName.Text = ejamaatregdtrdr.item("ITSFirstName").ToString()
    end if

3 个答案:

答案 0 :(得分:1)

DataReader需要调用Read来将自己定位在检索到的第一个记录

ejamaatregdtrdr = ejamaatregcmd.ExecuteReader()
If ejamaatregdtrdr.HasRows Then
     ejamaatregdtrdr.Read()
     txtFirstName.Text = ejamaatregdtrdr.item("ITSFirstName").ToString()
End if

顺便说一句,如果没有要读取的行,则Read返回false,因此您可以删除HasRows的测试并简单地写

ejamaatregdtrdr = ejamaatregcmd.ExecuteReader()
If ejamaatregdtrdr.Read() Then
     txtFirstName.Text = ejamaatregdtrdr.item("ITSFirstName").ToString()
End if

改进代码的另一个建议是开始使用Using Statement

Using Connection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;....")
Using ejamaatregcmd = new OleDbCommand("SELECT ITSData.[EJamaatID], ITSData.[ITSFirstName] FROM ITSData WHERE  EjamaatID= @EjamaatID", Connection)
    Connection.Open()
    ejamaatregcmd.Parameters.Add(New OleDbParameter("@EjamaatID", txtEjamaatID.Text))
    Using ejamaatregdtrdr = ejamaatregcmd.ExecuteReader()
        If ejamaatregdtrdr.Read() Then
           txtFirstName.Text = ejamaatregdtrdr.item("ITSFirstName").ToString()
       End if
    End Using
End Using
End Using           

using语句非常有用,可以帮助您关闭和处理一次性对象,如连接,命令和阅读器。缺乏正确的处理代码会占用更多内存并锁定资源,从而导致应用程序更加不稳定。

答案 1 :(得分:0)

在从DataReader读取数据之前,您需要通过调用Read方法将行移动到第一行。如果数据存在,则返回true,因此您无需检查HasRows属性:

ejamaatregdtrdr = ejamaatregcmd.ExecuteReader()
If ejamaatregdtrdr.Read() Then
    txtFirstName.Text = ejamaatregdtrdr.Item("ITSFirstName").ToString()
End if

答案 2 :(得分:0)

我通常会做类似以下的事情

Function GetResult(ID As string) As String
    GetResult = "No Data" 'Default Result
Dim conn As System.Data.OleDb.OleDbConnection = *NewConnectionObject*
Dim comm As System.Data.OleDb.OleDbCommand = *NewCommmandObject*
comm.Parameter.AddWithValue("@Parametername",ID)

    Using reader As System.Data.OleDb.OleDbDataReader = comm.ExecuteReader()
        If reader.HasRows Then
            'Reader has data, so iterate through it
            GetResult = ""
            while reader.read
               GetResult += reader("FirstName")
            End While
        Else
            'Either Do Nothing - Default Result will show
            'Throw New System.Exception("Empty Data") 'Try Catch Statement have overhead.. so it's not a popular methodology
            'Or Log Something.. 
        End If
    End Using
    If conn.state = connection.open Then
        conn.close
    End
End Function