簡體   English   中英

查詢表達式中的語法錯誤(逗號)?

[英]Syntax error (comma) in query expression?

Sooo 我目前正在編寫關於某些圖書館系統的第二年項目。 只是想快速詢問是否有人可以快速回答這個問題。 在我的一部分代碼中,在我的btnUpdate_Click下,顯然可以更新 Books 表中書籍的信息。 我在執行程序時收到一條錯誤消息,其中顯示Syntax error (comma) in query expression 這是我的代碼:

Imports System.Data.OleDb
Public Class BookDetails
Dim conn As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\msi\\Desktop\\UITM\\DIPLOMA CS110\\SEM 3\\CSC301\\groupproject\\ReadObrite\\ReadObrite\\User.accdb")
Dim dt As New DataTable
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter(cmd)

    Public Sub view()
        conn.Open()
        cmd.CommandType = CommandType.Text
        da = New OleDbDataAdapter("Select * from Books ", conn)
        da.Fill(dt)
        DataGridView1.DataSource = dt
        conn.Close()
    End Sub
    
    Private Sub BookDetails_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'UserDataSet.Books' table. You can move, or remove it, as needed.
        Me.BooksTableAdapter.Fill(Me.UserDataSet.Books)
        conn.Open()
        cmd.CommandType = CommandType.Text
        da = New OleDbDataAdapter("Select * from Books ", conn)
        da.Fill(dt)
        DataGridView1.DataSource = dt
        conn.Close()
    End Sub
    
    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        cmd = conn.CreateCommand()
        conn.Open()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "Update Books set ISBN = '" + TextBox12.Text + "' where Ttile = '" + TextBox11.Text + "', Author = '" + TextBox10.Text + "', Production = '" + TextBox9.Text + "', Category = '" + TextBox8.Text + "' and Year = '" + TextBox7.Text + "'"
        cmd.ExecuteNonQuery()
        conn.Close()
        btnSearch_Click(New Object, New EventArgs())
        view()
    End Sub
    
    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Me.BooksBindingSource.RemoveCurrent()
    End Sub
    
    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        cmd = conn.CreateCommand()
        conn.Open()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "Insert into Books values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "')"
        cmd.ExecuteNonQuery()
        MsgBox("New book is added")
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        TextBox6.Clear()
        conn.Close()
        conn.Open()
        cmd.CommandType = CommandType.Text
        da = New OleDbDataAdapter("Select * from Books ", conn)
        da.Fill(dt)
        DataGridView1.DataSource = dt
        conn.Close()
    End Sub
    
    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        TextBox6.Clear()
    End Sub
    
    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
    
    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        TextBox12.Text = DataGridView1.SelectedRows(0).Cells(0).Value.ToString()
        TextBox11.Text = DataGridView1.SelectedRows(0).Cells(1).Value.ToString()
        TextBox10.Text = DataGridView1.SelectedRows(0).Cells(2).Value.ToString()
        TextBox9.Text = DataGridView1.SelectedRows(0).Cells(3).Value.ToString()
        TextBox8.Text = DataGridView1.SelectedRows(0).Cells(4).Value.ToString()
        TextBox7.Text = DataGridView1.SelectedRows(0).Cells(5).Value.ToString()
    End Sub
    
    Private Sub refresh()
        If txtSearch.Text = "" Then
            view()
        End If
    End Sub
    
    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        cmd = conn.CreateCommand()
        conn.Open()
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "Select * From Books Where ISBN = '" + txtSearch.Text + "' or Title = '" + txtSearch.Text + "' "
        cmd.ExecuteNonQuery()
        da = New OleDbDataAdapter(cmd)
        dt = New DataTable
        da.Fill(dt)
        DataGridView1.DataSource = dt
        conn.Close()
        refresh()
    End Sub

End Class

我嘗試將所有逗號更改為“和”。 但隨后發生了新的錯誤。

嘗試這個:

cmd.CommandText = "Update Books set ISBN = '" + TextBox12.Text + "' WHERE Ttile = '" + TextBox11.Text + "' AND Author = '" + TextBox10.Text + "' AND Production = '" + TextBox9.Text + "' AND Category = '" + TextBox8.Text + "' AND [Year] = '" + TextBox7.Text + "'"

筆記:

  • Year 是一個 SQL 受保護的名稱,所以用 [] 括起來
  • 所有 SQL 偏角都不支持逗號
  • 正如 IFrank 所提到的,“Ttile”列名中可能有錯誤 - 可能會拋出無效的列名錯誤消息
  • 小心控制文本內容:使用 TextBoxXX.Text.replace("'","'") 清除它們或使用參數化查詢

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM