条件表达式中的 SQL 数据类型不匹配

Posted

技术标签:

【中文标题】条件表达式中的 SQL 数据类型不匹配【英文标题】:SQL Data type mismatch in criteria expression 【发布时间】:2016-04-15 01:47:30 【问题描述】:

运行下面的表达式时,我收到一条错误消息:

 Data type mismatch in criteria expression

消息框用于识别错误发生的位置,它到达检查点 1 然后停止!

                    Dim table2 As New DataTable
                    Dim recordcount2 As Integer
                    Dim command2 As String = "SELECT * FROM [Results] where " & "[TestID] = " & " '" & CInt(LocationID) & "'"
                    Dim adapter2 As New OleDb.OleDbDataAdapter(command2, conn)
                    table2.Clear()
                    MsgBox("Checkpoint 1")
                    recordcount2 = adapter2.Fill(table2)
                    MsgBox("Checkpoint 2")

代码在我程序的这一部分:

    Try
        'Defining variables
        Dim table As New DataTable
        Dim command As String
        Dim recordCount As Integer
        Dim LocationID As Integer
        command = "SELECT * FROM [Test] where " & "[MachineID] = " & " '" & machineID & "'" 'SQL command to find if there is a usename stored with that is in username text box

        Dim adapter As New OleDb.OleDbDataAdapter(command, conn) 'adapter
        table.Clear() 'adding data to a table.
        recordCount = adapter.Fill(table)
        If recordCount <> 0 Then
            For i = 0 To recordCount
                Try
                    LocationID = CInt(table.Rows(i)(0))
                    Dim table2 As New DataTable
                    Dim recordcount2 As Integer
                    Dim command2 As String = "SELECT * FROM [Results] where " & "[TestID] = " & " '" & CInt(LocationID) & "'"
                    Dim adapter2 As New OleDb.OleDbDataAdapter(command2, conn)
                    table2.Clear()
                    MsgBox("Checkpoint 1")
                    recordcount2 = adapter2.Fill(table2)
                    MsgBox("Checkpoint 2")
                    If recordcount2 <> 0 Then
                        For x = 0 To recordcount2
                            MsgBox("yay1")
                            Dim TestID As String = table2.Rows(x)(1)
                            Dim Thickness As String = table2.Rows(x)(2)
                            Dim TargetFilter As String = table2.Rows(x)(9)
                            Dim SNR As String = table2.Rows(x)(3)
                            Dim STD As String = table2.Rows(x)(4)
                            MsgBox("yay2")
                            Dim M1 As String = table2.Rows(x)(5)
                            Dim M2 As String = table2.Rows(x)(6)
                            Dim kVp As String = table2.Rows(x)(7)
                            Dim mAs As String = table2.Rows(x)(8)
                            MsgBox("yay3")
                            Dim CNR As Short = (CLng(M1) - CLng(M2)) / 2
                            MsgBox("Further")
                            dgvViewData.Rows.Add(TestID, Thickness, CStr(SNR), CStr(STD), CStr(M1), CStr(M2), kVp, mAs, CStr(CNR))
                        Next
                    Else
                        MsgBox("RIP")
                    End If
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
            Next
        Else
            MsgBox("There data for this machine.")
        End If
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

按钮上的代码

    Try
        Dim table As New DataTable
        Dim command As String
        Dim recordCount As Integer
        Dim TestNum As String = "1"
        command = "SELECT * FROM [Results] where " & "[TestID] = " & " '" & CStr(TestNum) & "'"
        Dim adapter As New OleDb.OleDbDataAdapter(command, conn)
        table.Clear()
        recordCount = adapter.Fill(table)
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

【问题讨论】:

【参考方案1】:

您的 locationID 参数必须是要连接到您的 select 语句中的字符串。我假设它是一个导致您的数据类型不匹配的整数。

【讨论】:

感谢您的快速响应,我将命令 2 更改为: Dim command2 As String = "SELECT * FROM [Results] where " & "[TestID] = " & " '" & CStr(LocationID ) & "'" 但是我仍然收到同样的错误!另外在访问时,TestID 是一个数字:gyazo.com/5965941b5d3a56678971e42dd5de84a0 您确定错误是该行代码的本地错误吗?尝试摆脱不需要的所有其他内容并再次运行它以确保错误不是来自脚本中的其他地方。另外,我注意到您将 [TestID] 参数化。没有理由,因为你在硬编码。 基本上我的代码在做什么,用户已经给出了 MachineID,我想要在 dataGridView 中列出该机器完成的所有结果,这是数据库关系的照片gyazo.com/11d9d441031fc76a8f0338d6f8339eff【参考方案2】:

我很确定您的 LocationID 是一个整数,因此应该按原样连接,因此 SQL 应该是:

Dim command2 As String = "SELECT * FROM [Results] where [TestID] = " & CStr(LocationID) & ""

【讨论】:

你好,我在OP的底部添加了一些代码,这段代码仍然返回同样的错误。 这不起作用,但是在访问时我将两个表上的 TestID 从“数字”更改为“短文本”,现在它可以工作了! 如果 TestID 是数字,它应该是数字,而不是文本。如果它是一个数字,我的代码必须工作,除非您的 LocationID 有 Null 值。

以上是关于条件表达式中的 SQL 数据类型不匹配的主要内容,如果未能解决你的问题,请参考以下文章

条件表达式 Oledb Access 数据库中的数据类型不匹配

错误“条件表达式中的数据类型不匹配”

条件表达式中的数据类型不匹配

MS Access - 条件表达式中的数据类型不匹配

条件表达式中的数据类型不匹配

条件表达式中的 C# 数据类型不匹配