错误IndexOutOfRangeException未处理

时间:2015-11-16 03:29:39

标签: vba ms-access-2010

Public Class Form1
    Dim provider As String
    Dim datafile As String
    Dim connstring As String
    Public myconnection As OleDbConnection = New OleDbConnection
    Public dr As OleDbDataReader

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        provider = "Provider=Microsoft.Ace.OLEDB.12.0"
        datafile = "Data Source=C:\Users\fess\Desktop\test\compress.accdb"
        connstring = provider & ";" & datafile
        myconnection.ConnectionString = connstring

    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        myconnection.Open()
        Dim str As String
        str = "SELECT 'name' FROM test2 WHERE 'ID'='1'"
        Dim cmd As OleDbCommand = New OleDbCommand(str, myconnection)
        dr = cmd.ExecuteReader
        TextBox1.Text = dr(str).ToString
        myconnection.Close()

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    myconnection.Open()
    Dim str As String
    str = "SELECT ID from test2 where name='bry'"
    Dim cmd As OleDbCommand = New OleDbCommand(str, myconnection)
    dr = cmd.ExecuteReader
    While dr.Read()
        TextBox1.Text = dr("ID").ToString
    End While
    myconnection.Close()

经过一番搜索后发现我的错误