开发者

Extracting data from a database using OleDbConnection

Dim con As New OleDb.OleDbConnection
Public outputs As New DataSet    
sub main() 
    sqlquery("SELECT * FROM Students", "mark")
end sub
Sub sqlquery(ByVal sql As String, ByVal outputname As String)
    Try
        con.Open()
        Dim da As OleDb.OleDbDataAdapter
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(outputs, outputname)
        con.Close()
    Catch
        errorid = 3
    End Try
    Console.WriteLine(messagefromerrorcode)
    Console.WriteLine(outputs.Tables(outputname).Rows(0).Item(1))
End Sub

Here is an image of my table da开发者_运维百科ta and design

I am trying to read some values from a database using the oledb dataset however when I run the sql command and get down to outputting the value I am greeted with the following error: "IndexOutOfRangeException: There is no row at position 0.". Yet when I run the very same sql command in access I get back the data I wanted. When I am running the above code, no errors are produced until I get down to the last writeline where I want to output my data.

Any ideas would be a great help.


I don't see any connection string getting assigned to the OleDbConnection object con. Did you edit that out for this example?

This looks messed up to me because OleDbDataAdapter.Fill is for filling a data set from a record set after you have filled ther record set from the database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜