【问题标题】:Data type mismatch in criteria expression. MS Access VB条件表达式中的数据类型不匹配。微软访问 VB
【发布时间】:2013-10-07 18:20:02
【问题描述】:

'确定按钮

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
    Dim con As New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;data source=C:\Users\Jill\Desktop\saddbase\Sadsystem\Sadsystem\bin\Debug\tenant.mdb")
    Dim cmd As OleDbCommand = New OleDbCommand("SELECT * FROM info WHERE TN_ID = '" & UsernameTextBox.Text & "' AND Password = '" & PasswordTextBox.Text & "' ", con)
    con.Open()
    Dim sdr As OleDbDataReader = cmd.ExecuteReader()
    ' If the record can be queried, Pass verification and open another form.  
    If (sdr.Read() = True) Then
        MessageBox.Show("The user is valid!")

        Me.Hide()
    Else
        MessageBox.Show("Invalid Tenant ID or password!")


End If

当我运行程序时,cmd.ExecuteReader() 中出现错误。 标准表达式中的数据类型不匹配请帮助解决此错误。

【问题讨论】:

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


    【解决方案1】:

    在您的查询中,您为 TN_ID 和密码字段传递了两个字符串。
    TN_ID 可能是一个数字字段,你不需要在它周围加上引号,我觉得你传递 UserName 文本框的值真的很奇怪。

    也就是说,我希望检查您的查询,因为您还没有看到潜在的问题:

    首先,PASSWORD 是保留关键字,因此您需要在其周围使用方括号。
    其次,不要使用字符串连接来构建sql命令,而是使用像这样的参数化查询

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
    
        Dim commandText = "SELECT * FROM info WHERE TN_ID = ? AND [Password] = ?"
        Using con = New OleDbConnection(......))
        Using cmd = New OleDbCommand(commandText,con))
           con.Open()
    
           ' If the TN_ID is really a numeric field then you need '
           ' to conver the first parameter to a number '
           ' cmd.Parameters.AddWithValue("@p1", Convert.ToInt32(UsernameTextBox.Text))'
    
           cmd.Parameters.AddWithValue("@p1", UsernameTextBox.Text)
           cmd.Parameters.AddWithValue("@p2", PasswordTextBox.Text)
           Using sdr As OleDbDataReader = cmd.ExecuteReader())
            .....
           End Using
        End Using
        End Using    
    End Sub
    

    作为旁注,与您的问题无关,请考虑不要将密码以纯文本形式存储在数据库中。有一些技术可以对密码文本进行哈希处理并将结果存储在数据库中。通过这种方式,没有人可以简单地查看数据库文件来获取密码。 See the details in this question

    【讨论】:

      【解决方案2】:
      Private Sub SumOfIR()
      
          Try
              Dim con As New System.Data.OleDb.OleDbConnection(ConnectionString)
              Dim com As New System.Data.OleDb.OleDbCommand
      
              con.Open()
              com.Connection = con
              com.CommandText = "Select Sum(IR) from Spectrum where StdNu='" + TxtNuTeif.Text + "'"
              com.Parameters.Clear()
              Dim SumIR As OleDbDataReader = com.ExecuteScalar
              LblIRTeif.Text = com.ExecuteScalar("SumIR").ToString
              con.Close()
              com.Dispose()
      
          Catch ex As Exception
              BehComponents.MessageBoxFarsi.Show(ex.ToString, "", BehComponents.MessageBoxFarsiButtons.OK, MessageBoxIcon.Warning)
      
          End Try
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-07
        • 2017-01-14
        • 1970-01-01
        • 2016-01-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多