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

jaxagkaj  于 2023-01-31  发布在  SQLite
关注(0)|答案(1)|浏览(175)

我有一个问题,在此代码中,我需要在数据库SQLite中搜索vb.net当我运行此代码,它开始很好,但当搜索我有此消息错误(数据绑定错误SQL逻辑错误附近“喜欢”语法错误vb.net)Error我的代码:

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

当我运行这段代码时,它的开始很好,但当搜索时,我有这个消息错误(数据绑定错误SQL逻辑错误靠近“like”语法错误vb.net)Error

68bkxrlz

68bkxrlz1#

正如其他人所建议的,我们需要在where子句之前给予空间。
请进行以下更改:

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

相关问题