VB.NET - .Fill命令 - 未处理的异常

时间:2016-05-26 06:09:05

标签: vb.net

我尝试使用.Fill

时,会收到其中一条非常有用的错误消息
    Public Class SQLSearch
Dim CON As New OleDb.OleDbConnection                'Database Connection object
Dim CONSTR As String                                'Connection String
Dim dbProvider As String                            'Database Provider
Dim dbSource As String                              'Data Source
Dim Database As String                              'DB Name
Dim strSEARCH As String                             'Variable held by txtName.text
' Dim SQL As String                                   'SQL Query
Dim NAME1 As String
Dim STREET1 As String
Dim SUBURB1 As String
Dim PCODE1 As Integer
Dim HOMEPH1 As Integer
Dim MOBPH1 As Integer
Dim EMAIL1 As String

'public CONNECT As New System.Data.SqlClient.SqlConnection(“Integrated Security=SSPI;Initial Catalog=SMART2003.mdb;Data Source=(local);”)

Public Sub AccessCustDetails()
    'Define The Provider
    dbProvider = "PROVIDER=Microsoft.ACE.OLEDB.15.0;"

    'Define Database Name
    Database = "\smart2003.mdb"

    'Define Data Source
    dbSource = "C:\Smart\SMART2003.mdb"

    'Define Connection string - Provider + Data Source
    CON.ConnectionString = "PROVIDER=Microsoft.ACE.OLEDB.15.0; data source = C:\Smart\SMART2003.mdb;"

    'Open Database connection
    CON.Open()

    'Define Search Term
    strSEARCH = frmnewcust.txtName.Text

    'Define SQL string
    'SQL = "Select * From tblcustdet Where customername = " & "'" & strSEARCH & "'"

    MsgBox("1ok")
    MsgBox("The value of strSEARCH is " & strSEARCH)

    Dim CMD As New OleDb.OleDbCommand
    Dim SQL As String = "SELECT * FROM tblCustDet WHERE CustomerName= ?"

    If strSEARCH = "" Then
        MsgBox("Please enter customer's name.", MsgBoxStyle.Exclamation, "Add New Customer")

    Else
        Using CON As New OleDb.OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.15.0; data source = C:\Smart\SMART2003.mdb;")
            CMD = New OleDb.OleDbCommand(SQL, CON)
            CON.Open()
            'MsgBox("Connection to database is " & CON.State)
            CMD.Parameters.AddWithValue("@CustomerName", strSEARCH)
            Dim RESULTS = Convert.ToInt32(CMD.ExecuteScalar)
            'If the Results number is greater than 0 then Customer already exists
            If RESULTS > 0 Then
                MsgBox("Customer name already exists.", MsgBoxStyle.Exclamation, "New Customer Record")

            Else

                MsgBox("2ok")

                NAME1 = My.Forms.frmnewcust.txtName.Text
                STREET1 = My.Forms.frmnewcust.txtStreet.Text
                SUBURB1 = My.Forms.frmnewcust.txtSuburb.Text
                PCODE1 = My.Forms.frmnewcust.txtPCode.Text
                HOMEPH1 = My.Forms.frmnewcust.txtHomePh.Text
                MOBPH1 = My.Forms.frmnewcust.txtMob.Text
                EMAIL1 = My.Forms.frmnewcust.txtEmail.Text

                Dim DS As New DataSet                               'Dataset Object
                Dim DA As OleDb.OleDbDataAdapter                    'Data Adapter Object
                Dim DSNEWROW As DataRow
                Dim TABLE As String

                TABLE = "tblCustDet"

                DA = New OleDb.OleDbDataAdapter(SQL, CON)

                DA.Fill(DS, TABLE)

                Dim COMBUILD As New OleDb.OleDbCommandBuilder(DA)
                DSNEWROW = DS.Tables(TABLE).NewRow

                DS.Tables(TABLE).Rows.Add(DSNEWROW)

                DSNEWROW.Item("CustomerName") = NAME1
                DSNEWROW.Item("STREET") = STREET1
                DSNEWROW.Item("suburb") = SUBURB1
                DSNEWROW.Item("PostCode") = PCODE1
                DSNEWROW.Item("LandlineNumber") = HOMEPH1
                DSNEWROW.Item("MobileNumber") = MOBPH1
                DSNEWROW.Item("EmailAddress") = EMAIL1

                DA.Update(DS, TABLE)

                MsgBox("Customer Added Successfuly.", MsgBoxStyle.OkOnly)

            End If
        End Using
    End If
    CON.Close()
    MsgBox("3ok")
End Sub
End Class

DA.Fill(DS, TABLE)我得到了:

未处理的类型' System.Data.OleDb.OleDbException'发生在System.Data.dll

附加信息:没有给出一个或多个必需参数的值。

TABLE变量在我的数据库中具有正确的表名。

有人可以给我一些暗示我缺少的东西吗?

编辑以显示整个代码

1 个答案:

答案 0 :(得分:1)

这是你的问题:

DA = New OleDb.OleDbDataAdapter(SQL, CON)
DA.Fill(DS, TABLE)

使用sql查询和连接创建OleDbDataAdapter。您的查询
Dim SQL As String = "SELECT * FROM tblCustDet WHERE CustomerName= ?"使用参数。但是当您使用OleDbDataAdapter执行查询时,未添加参数 在第一个“搜索”执行参数已提供,一切正常

Using command As New OleDbCommand(SQL, CON)
    command.Parameters.AddWithValue("@CustomerName", strSEARCH)
    Using adapter As New OleDbDataAdapter(command)
         adapter.Fill(DS, TABLE)
    End Using
End Using