查询中的语法错误 [英] Syntax error in query

查看:185
本文介绍了查询中的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想要做的是,如果我从前两个组合框中选择值并单击搜索,它应该在第三个文本框中显示结果。我正在使用Ms Access作为我的数据库。





在VB中编码以链接到数据库



So, what i am trying to do is, if i select values from the first two combo box and click search it should display the result in third text box. I am using Ms Access as my database.


coding in VB to link to DB

Imports System.Data.OleDb
Public Class Form1
    Dim v_HardnessDbconn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;DataSource=C:\Users\Desktop\Hardness.accdb")
    Dim v_datareader As OleDbDataReader

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        v_HardnessDbconn.Open()
        v_HardnessDbconn.Close()
    End Sub





搜索框上的编码



coding on the search box

    Private Sub searchtext_Click(sender As Object, e As EventArgs) Handles searchtext.Click
        v_HardnessDbconn.Open()
        TextBox1.Clear()
        Dim Str As String

        Str = "SELECT * From Items Where (Material = '" & ComboBox1.Text & "', Hardness = '" & ComboBox2.Text & "')"
        Dim v_dbCommand As OleDbCommand

        v_dbCommand = New OleDbCommand(Str, v_HardnessDbconn)
        Dim v_dataReader As OleDbDataReader = v_dbCommand.ExecuteReader()
        If v_dataReader.HasRows Then
            While v_dataReader.Read
                TextBox1.Text = v_dataReader("Material Code").ToString

            End While
            v_HardnessDbconn.Close()
        End If


    End Sub
End Class





运行后出错



error getting after running it

An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Syntax error (comma) in query expression '(Material = 'Plain Carbon Steel ( AISI 1010- AISI 1030)', Hardness = '150-180')'.





有人可以提供帮助。

我在这个表达式中是否有一些错误,我选择了两个值不同的组合框,然后在数据库中读取这些值以显示我的结果:





Can someone please help.
Is there some mistake i made in this expression, where i am choosing values from two different combo boxes and then reading those values in the database to display my result :

Str = "SELECT * From Items Where (Material = '" & ComboBox1.Text & "', Hardness = '" & ComboBox2.Text & "')"





或我的数据库出了什么问题?



or is something wrong with my database?

推荐答案

你好,



你需要条件之间的逻辑关系, OR / AND 等。



Hello,

You need a logical relationship between your condition, OR/AND etc.

Str = "SELECT * From Items Where Material='" & ComboBox1.Text & "' AND Hardness='" & ComboBox2.Text & "'"





会工作......



Will work...


除了解决方案1 ​​...



请不要使用命令像这样:

In addition to solution 1...

Please, do not use command like this:
Str = "SELECT * From Items Where Material='" & ComboBox1.Text & "' AND Hardness='" & ComboBox2.Text & "'"





而不是它,使用参数化查询:



Rather than it, use parameterized query:

Str = "SELECT * From Items Where Material=? AND Hardness=?"





如何使用它?

请参阅: OleDbParameterCollection.Add方法(OleDbParameter) [ ^ ]


这篇关于查询中的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆