vb.net使用Odbc(ERROR)读取和写入数据库到ms数据库

时间:2021-04-24 15:42:52

When I try to run the program, an error shows " ERROR[07002][Microsoft][ODBC MICROSOFT ACCESS DRIVER] TOO FEW PARAMETERS.EXPECTED 1" USING THIS CONNECTION.

当我尝试运行该程序时,错误显示“错误[07002] [Microsoft] [ODBC MICROSOFT ACCESS DRIVER] TOO FEW PARAMETERS.EXPECTED 1”使用此连接。

          Public Sub open_connection ()
          Try
             con = New OdbcConnection("dsn = LocalDB")
             con.Open()
          End try
         Catch ex As Exception
            MsgBox(ex.message)
         End sub

Problems occurs when inserting and reading..

插入和读取时出现问题..

          sSql = "select * from Faculty where RFID='" & txtrfid.Text & "'"
                Dim cmd As New OdbcCommand(sSql, con)
                Dim dr As OdbcDataReader = cmd.ExecuteReader()
                If dr.HasRows Then
                    dr.Read()
                    txtfname.Text = dr("fname").ToString()
                    txtlname.Text = dr("lname").ToString()
                    txtid.Text = dr("STID").ToString()
                    txtposition.Text = dr("Pstion").ToString()
                    txtsubject.Text = dr("Subject").ToString()
                    Dim bits As Byte() = CType(dr("Pfile"), Byte())
                    Dim memo As New MemoryStream(bits)
                    Dim myimg As New Bitmap(memo)
                    imgRetrieve.Image = myimg
                    dr.Close()

                    sSql = " SELECT TOP 1 flag_inout FROM Attendance where faculty_id = @StId ORDER BY Attendance_id DESC"
                    Dim pcmd As New OdbcCommand(sSql, con)
                    pcmd.Parameters.AddWithValue("@StId", txtid.Text)
                    Dim pdr As OdbcDataReader = pcmd.ExecuteReader()
                    While pdr.Read()
                        TextBox1.Text = pdr("flag_inout").ToString()

                    End While
                    If TextBox1.Text = "IN" Then
                        TextBox1.Text = "OUT"

                    ElseIf TextBox1.Text = "OUT" Then
                        TextBox1.Text = "IN"

                    ElseIf TextBox1.Text = Nothing Then
                        TextBox1.Text = "IN"

                    End If

                    pdr.Close()
                    'Check Duplicate
                    'If txtfname.Text.Length = 0 Then
                    '    Return
                    'End If
                    'If Not CheckDateDuplicate() Then
                    '    MessageBox.Show("Already Saved on this Date")
                    '    Return
                    'End If

                    sSql = "insert into Attendance (faculty_id, faculty_Fname,faculty_Lname,[Position],Subject, attendance_date, attendance_time, flag_inout) values('" & txtid.Text & "','" & txtfname.Text & "','" & txtlname.Text & "','" & txtposition.Text & "','" & txtsubject.Text & "','" & DateTimePicker1.Value.Date & "','" & TimeOfDay.ToShortTimeString & "','" & TextBox1.Text & "')"

                    Dim xcmd As New OdbcCommand(sSql, con)
                    If xcmd.ExecuteNonQuery() > 0 Then

                        'MessageBox.Show("ATTENDANCE SAVED")
                        txtrfid.Text = ""
                        'txtid.Text = ""
                        'txtfname.Text = ""
                        'txtlname.Text = ""
                        'txtposition.Text = ""
                        'txtsubject.Text = ""
                        txtrfid.Focus()
                        Dtagrid()
                    End If

                    If TextBox1.Text = "IN" Then
                        lblinuse.Visible = True
                        lblvacant.Visible = False
                        lbllstuser.Visible = False
                        lblcrrntuser.Visible = True
                    ElseIf TextBox1.Text = "OUT" Then
                        lblinuse.Visible = False
                        lblvacant.Visible = True
                        lbllstuser.Visible = True
                        lblcrrntuser.Visible = False
                        'txtid.Text = ""
                        'txtfname.Text = ""
                        'txtlname.Text = ""
                        'txtposition.Text = ""
                        'txtsubject.Text = ""
                        'imgRetrieve.Image = Nothing
                    End If
                End If

                Dtagrid()


            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
  End Sub

1 个解决方案

#1


0  

CAUSE

This error occurs only with Microsoft Access when one of the column names specified in a select statement does not exist in the table being queried.

只有在查询的表中不存在select语句中指定的列名之一时,才会出现此错误。

Resolution

Remove any invalid column names from the select statement.

从select语句中删除任何无效的列名称。

Make sure that Column Name RFID exist in Faculty table and also check txtrfid has value or not

确保Faculty表中存在Column Name RFID,并检查txtrfid是否具有值

#1


0  

CAUSE

This error occurs only with Microsoft Access when one of the column names specified in a select statement does not exist in the table being queried.

只有在查询的表中不存在select语句中指定的列名之一时,才会出现此错误。

Resolution

Remove any invalid column names from the select statement.

从select语句中删除任何无效的列名称。

Make sure that Column Name RFID exist in Faculty table and also check txtrfid has value or not

确保Faculty表中存在Column Name RFID,并检查txtrfid是否具有值