system.data.sqlclientexception:'@ p2'附近的语法不正确

问题描述

我试图在必须更新数据库中数据的VB.net Windows应用程序中插入,更新Grid上的数据。我无法使用新的架构名称(客户)来实现此功能,但是当我尝试使用架构“ dbo”创建表时,我可以插入网格并更新数据,也可以在sql Server中查看数据。 / p>

请帮助我修改代码所需的内容,以执行插入和更新选项。

代码

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

    Dim test1 As String
    test1 = "Select * from Customer.CustomerID"

    connection = New OleDbConnection
    connection.ConnectionString = "Provider=MSOLEDBsql.1;Integrated Security=sspI;Initial Catalog=prod;Data Source=IN-TESTVM;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=IN-TESTVM;Use Encryption for Data=False;Tag with column collation when possible=False;MARS Connection=False;DataTypeCompatibility=0;Trust Server Certificate=False;Application Intent=READWRITE;MultisubnetFailover=False;Use FMTONLY=False;"
    connection.open()
    myDA = New OleDbDataAdapter(test1,connection)
    dsDataGrid = New DataSet
    myDA.Fill(dsDataGrid)
    grid.DataSource = dsDataGrid.Tables(0)
    bindsrc2.DataSource = dsDataGrid
    connection.Close()

End Sub

按钮点击事件代码如下

Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click
    app = New OleDbCommandBuilder(myDA)
    bindsrc2.EndEdit()

    myDA.Update(bindsrc2.DataSource)  'Hitting the error while updating the data at this line
End Sub

我也可以使用dbo以外的模式名称将数据加载到网格中。

myDA.Fill(dsDataGrid)
grid.DataSource = dsDataGrid.Tables(0)
bindsrc2.DataSource = dsDataGrid

点击保存按钮后出现错误消息

enter image description here

解决方法

尝试将命令构建器的QuotePrefixQuoteSuffix分别设置为"[""]"。在查询中使用通配符时,命令构建器不会自动转义列名,这意味着关键字或空格或其他特殊字符将导致语法错误。

请注意,有两种选择。一种是根本不使用命令构建器,而是创建自己的操作命令。在这种情况下,您将编写SQL,以便转义需要它的列名称。另一个是不要在查询中使用通配符,在这种情况下,您将转义需要通配符的列名,并且命令构建器将紧随其后。