Visual Basic.Net 2010异常错误含义

时间:2014-06-10 16:07:23

标签: vb.net visual-studio-2010 ms-access-2007

任何人都可以帮我弄清楚这个错误陈述的含义是什么。我一直得到这个错误陈述: -

index(从零开始)必须大于或等于零且小于参数列表的大小

以下是我的编码

Imports System.Data.OleDb
Public Class form2


    Dim Mycn As OleDbConnection
    Dim Command As OleDbCommand
    Dim icount As Integer
    Dim SQLstr As String

    Private Sub Button1_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try
            Mycn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\lenovo\Documents\Final year stuff\BookStoreDb.mdb;")
            Mycn.Open()

            SQLstr = String.Format("INSERT INTO login VALUES('{0}','{1}','{2}','{3}','{4}')", TextBox1.Text, TextBox2.Text)
            Command = New OleDbCommand(SQLstr, Mycn)


            icount = Command.ExecuteNonQuery
            MessageBox.Show(icount)


        Catch ex As Exception
            MessageBox.Show(ex.Message & " - " & ex.Source)
            Mycn.Close()
        End Try
    End Sub



    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Me.Close()

    End Sub

End Class

1 个答案:

答案 0 :(得分:0)

正如Plutonix建议的那样,只需指定两个字段。

您可以将其与参数结合使用,这样您也可以免受注射攻击。

尝试以下方法:

SQLstr = String.Format("INSERT INTO Login (User, Password) VALUES ('{0}','{1}')", TextBox1.Text, TextBox2.Text)
Command = New OleDbCommand(SQLstr, Mycn)