SQL命令错误'和'附近的语法不正确

时间:2017-08-24 19:47:03

标签: sql-server vb.net winforms

我是VB.net的新手学生,正在努力学习一个我无法弄清楚如何解决的表格。表单是一个搜索表单,可以根据四个输入搜索资产表。用户可以选择要提供的输入。表单的界面看起来像我附上的图片here。问题是,当我勾选任何复选框时,它会在关键字'和'错误附近给出错误的语法。

也非常鼓励采用任何其他方法来达到预期的效果。

我正在使用以下代码

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    sCommand = New SqlCommand("", con)
    Dim script As String
    script = "Select * from tblassets where [asset_costprice] like %"
    If checkassetsupplier.Checked = False AndAlso checkassetstatus.Checked = False AndAlso CheckassetID.Checked = False AndAlso checkassetsupplier.Checked = False Then
        sCommand.CommandText = "Select * from tblassets"
    End If
    If checkassetsupplier.Checked = True Then
        script = script & " and [Asset_supplier] = @p1"
        sCommand.Parameters.AddWithValue("@p1", cbsearchsupplier.Text)
    End If
    If checkassetstatus.Checked = True Then
        script = script & " and asset_status = @p2"
        sCommand.Parameters.AddWithValue("@p2", cbsearchstatus.SelectedIndex)
    End If
    If CheckassetID.Checked = True Then
        script = script & " and asset_id = @p3"
        sCommand.Parameters.AddWithValue("@p3", tbsearchassetid.Text)
    End If
    If checkassetname.Checked = True Then
        script = script & " and asset_name = @p4"
        sCommand.Parameters.AddWithValue("@p4", tbsearchassetname.Text)
    End If
    sCommand.CommandText = script
    sAdapter = New SqlDataAdapter(sCommand)
    sBuilder = New SqlCommandBuilder(sAdapter)
    sDs = New DataSet()
    sAdapter.Fill(SMSSDataSet, SMSSDataSet.tblassets.TableName)
    Dim dt As New DataTable
    With dt
        .Columns.Add("No", GetType(Integer))
        .Columns.Add("Status", GetType(String))
        .Rows.Add("0", "Held")
        .Rows.Add("1", "Depreciated")
        .Rows.Add("2", "Partially Sold")
        .Rows.Add("3", "Sold")
    End With
    dgvsearch.AutoGenerateColumns = False
    dgvsearch.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "Asset ID", .DataPropertyName = "asset_id", .ReadOnly = True, .AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader})
    dgvsearch.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "Name", .DataPropertyName = "asset_name", .ReadOnly = True, .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill})
    dgvsearch.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "Price", .DataPropertyName = "unit_price", .ReadOnly = True, .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill})
    dgvsearch.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "Supplier", .DataPropertyName = "asset_supplier", .ReadOnly = True, .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill})
    dgvsearch.Columns.Add(New DataGridViewComboBoxColumn() With {.HeaderText = "Status", .DataSource = dt, .DisplayMember = "Status", .ValueMember = "No", .DataPropertyName = "asset_status", .ReadOnly = True, .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill})
    dgvsearch.Columns.Add(New DataGridViewTextBoxColumn() With {.HeaderText = "Quantity", .DataPropertyName = "quantity", .ReadOnly = True, .AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill})

    dgvsearch.DataSource = SMSSDataSet.tblassets


End Sub

1 个答案:

答案 0 :(得分:1)

只是在黑暗中拍摄,但是你的剧本使用了但是并没有说它应该是什么样的,并且没有任何' '显示相似的陈述是什么。

https://www.w3schools.com/sql/sql_like.asp

可能会改变这一点,看看是否有帮助:

 script = "Select * from tblassets where [asset_costprice] like '%'"

我也会改变你所有的ifs =' '同样,看看现在发生了什么错误。