(SQLite vb.net)数据绑定错误 “喜欢”附近的 SQL 逻辑错误 语法错误 vb.net

(SQLite vb.net)data binding error sql logic error near "like" syntax error vb.net

提问人:Mohamed Magdy 提问时间:1/28/2023 最后编辑:Rohad BokharMohamed Magdy 更新时间:1/30/2023 访问量:92

问:

我在这个代码中有一个问题 我需要使用 vb.net 在数据库中搜索SQLite 当我运行此代码时,它启动良好,但是在搜索时,我收到此消息 错误(数据绑定错误,sql逻辑错误,接近“like”语法错误,错误 vb.net),错误我的代码:

Imports System.Data.SQLite

Public Class Form1

    Private dbcomand As String = ""
    Private bindingSrc As BindingSource

    Private dbName As String = "conquest.db3;"
    Private dbPath As String = "D:\conquestdicomserver\data\dbase\" & dbName
    Private conString As String = "Data Source=" & dbPath & "Version=3;New=False;Compress=True;"

    Private connection As New SQLiteConnection(conString)
    Private command As New SQLiteCommand("", connection)

    Private sql As String = ""

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        connection.Open()

        UpdateDatabiding()

        connection.Close()


    End Sub

    Private Sub UpdateDatabiding(Optional cmd As SQLiteCommand = Nothing)
        ''TODO
        Try
            If cmd Is Nothing Then
                command.CommandText = "SELECT StudyDate,PatientNam,StudyModal,StudyDescr,PatientsAg,PatientID FROM DICOMStudies"
            Else
                command = cmd
            End If

            Dim adapter As New SQLiteDataAdapter(command)
            Dim dataSt As New DataSet()
            adapter.Fill(dataSt, "DICOMStudieList")

            bindingSrc = New BindingSource()
            bindingSrc.DataSource = dataSt.Tables("DICOMStudieList")

            Dim tb As TextBox
            For Each ctr As Control In GroupBox1.Controls
                If TypeOf ctr Is TextBox Then
                    tb = CType(ctr, TextBox)
                    tb.DataBindings.Clear()
                    tb.Text = ""
                End If

            Next


            IDShowTextBox.DataBindings.Add("Text", bindingSrc, "PatientID")
            NameShowTextBox.DataBindings.Add("Text", bindingSrc, "PatientNam")

            DataGridView1.Enabled = True
            DataGridView1.DataSource = bindingSrc
            DataGridView1.AutoResizeColumns(CType(DataGridViewAutoSizeColumnsMode.AllCells, DataGridViewAutoSizeColumnsMode))
            DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect

            DataGridView1.Columns(0).Width = 60
            DisplayPosition()

        Catch ex As Exception
            MessageBox.Show("Data Binding Error: " & ex.Message.ToString())
        End Try


    End Sub

    Private Sub DisplayPosition()
        PositionLabel1.Text = "Position:  " & bindingSrc.Position + 1 & "/" & bindingSrc.Count
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim Psi As New ProcessStartInfo
        With Psi
            .FileName = "C:\Program Files\RadiAntViewer64bit\RadiAntViewer.exe"
            .Arguments = "D:\conquestdicomserver\data\" & IDShowTextBox.Text
        End With
        Process.Start(Psi)

    End Sub

    Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click

        If connection.State = ConnectionState.Closed Then
            connection.Open()
        End If

        Try
            If String.IsNullOrEmpty(KeywordTextBox.Text.Trim()) Then
                UpdateDatabiding()
                Exit Sub
            End If

            sql = "SELECT StudyDate,PatientNam,StudyModal,StudyDescr,PatientsAg,PatientID FROM DICOMStudies"
            sql &= "WHERE PatientNam LIKE @keyword2"


            command.CommandType = CommandType.Text
            command.CommandText = sql
            command.Parameters.Clear()

            Dim KeywordString As String = String.Format("%{0}%", KeywordTextBox.Text)
            command.Parameters.AddWithValue("@keyword1", KeywordTextBox.Text)
            command.Parameters.AddWithValue("@keyword2", KeywordString)
            UpdateDatabiding(command)


        Catch ex As Exception
            MessageBox.Show("Search Error: " & ex.Message.ToString(),
                            "Error Message : iBasskung Tutorial.",
                            MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            connection.Close()
            KeywordTextBox.Focus()
        End Try

    End Sub
End Class

当我运行此代码时,它启动良好,但是在搜索时,我收到此消息 错误(数据绑定错误、“like”语法附近的 sql 逻辑错误、语法错误 vb.net) 错误

数据库 vb.net visual-studio sqlite visual-studio-code

评论

1赞 jmcilhinney 1/28/2023
当您不费心调试代码时,就会发生这种情况。正确使用调试器,即设置断点,然后查看实际值,问题应该很明显。如果你被告知你的 SQL 中存在语法错误,而你没有查看你的 SQL,那么你就忽略了你面前的信息。这也是为什么通常应避免字符串连接的一个例子。发现问题应该不难,但你让你的代码更难阅读,这就是你错过它的原因。sql
0赞 ClearlyClueless 1/28/2023
Tldr:表名和 WHERE 之间没有空格,clause.in 你错过的 SQL 字符串,请确保它连接在一起并在不同的行上。

答:

0赞 Jack J Jun 1/30/2023 #1

正如其他人所抱怨的那样,我们需要在从句之前留出空间。where

请进行以下更改:

    sql = "SELECT StudyDate,PatientNam,StudyModal,StudyDescr,PatientsAg,PatientID FROM DICOMStudies"
    sql &= " WHERE PatientNam LIKE @keyword2" //-> add a space before WHERE