【问题标题】:vb.net Read and write data to ms database using Odbc(ERROR)vb.net 使用 Odbc(ERROR) 读取和写入数据到 ms 数据库
【发布时间】:2014-03-02 03:21:47
【问题描述】:

当我尝试运行该程序时,错误显示“错误[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

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

          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

【问题讨论】:

  • 这段代码容易受到sql注入攻击。它实际上是在乞求被黑。

标签: vb.net visual-studio-2010 .net-4.0


【解决方案1】:

原因

此错误仅发生在 Microsoft Access 中,当其中一个列 表中不存在 select 语句中指定的名称 被查询。

分辨率

从 select 语句中删除所有无效的列名。

确保 Faculty 表中存在列名 RFID 并检查 txtrfid 是否有价值

【讨论】:

  • 当我使用 oledb .. 我工作正常,但当我将其更改为 Odbc.. 问题显示,,,该怎么办?
  • 这只是关于桌子的。检查字段
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-22
  • 1970-01-01
  • 1970-01-01
  • 2013-12-14
  • 2011-03-28
  • 1970-01-01
  • 2022-12-11
相关资源
最近更新 更多