JOIN操作中的语法错误

时间:2015-10-29 09:49:53

标签: sql vb.net join ms-access-2010 oledb

我有一个搜索按钮,可以搜索4个表格。所以我使用三个内部进行了Sql查询,并且我有CheckBoxes,因此如果选中了CheckBox,它会将条件添加到查询中。但我有一个问题是:

  

System.Data.OleDb.OleDbException
  JOIN操作中的语法错误

    cmd = New OleDbCommand(Sql, myConnection)
    Dim da As OleDbDataAdapter

    Dim sql = "SELECT * from (tblDopages AS A INNER JOIN tblMatrice AS B on A.strMatrice = B.strMatrice) INNER JOIN (tblRefMatrice AS C on B.strMatrice = C.strMatrice) INNER JOIN (tblRefMatDetails AS D on C.intIDref = D.intIDRef) WHERE "

    If CKBrefech.Checked = True Then
        sql = sql & "strRefEch = '" & TBrefech.Text & "'  AND "
    End If
    If CKBmethode.Checked = True Then
        sql = sql & "strMethode = '" & CBmethode.Text & "' AND "
    End If
    If CKBpurif.Checked Then
        sql = sql & "strPurif = '" & CBpurif.Text & "' AND "
    End If
    If CKBmatrice.Checked Then
        sql = sql & "strMatrice = '" & CBmatrice.Text & "' AND "
    End If
    If CKBmol.Checked Then
        sql = sql & "strMolecule = '" & CBmol.Text & "' AND "
    End If
    If CKBechprep.Checked Then
        sql = sql & "datDatePrepa >= @DatPrepa AND "
        cmd.Parameters.Add("@DatPrepa", OleDbType.Date).Value = DTPechprep.Value.Date
    End If
    If CKBechau.Checked Then
        sql = sql & "datDatePrepa <= @Datau AND "
        cmd.Parameters.Add("@Datau", OleDbType.Date).Value = DTPechau.Value.Date
    End If
    If CKBtrigprepa.Checked = True Then
        sql = sql & "strTrigPrepa = '" & TBtrigprepa.Text & "' AND "
    End If
    If CKBtriganaly.Checked = True Then
        sql = sql & "strTrigAnaly = '" & TBtrigAnaly.Text & "' AND "
    End If
    If CKBappar.Checked = True Then
        sql = sql & "strNomTech = '" & CBappar.Text & "' AND "
    End If
    If CKBnumappar.Checked = True Then
        sql = sql & "[strEquip(Appareil)] = '" & CBnumappar.Text & "' AND "
    End If
    If CKBteneurmini.Checked = True Then
        sql = sql & "dblDopage >= " & TBteneurmini.Text & " AND "
    End If
    If CKBteneurmax.Checked = True Then
        sql = sql & "dblDopage <= " & TBteneurmax.Text & " AND "
    End If

    If GroupBox1.Enabled Then
        Try
            If CKBnomref.Checked Then
                sql = sql & "D.strReferentiel = '" & CBnomref.Text & "' AND "
            End If
            If CKBniv1.Checked Then
                sql = sql & "D.strNIveau1 = '" & CBniv1.Text & "' AND "
            End If
            If CKBniv2.Checked Then
                sql = sql & "D.strNiveau2 = '" & CBniv2.Text & "' AND "
            End If
            If CKBniv3.Checked Then
                sql = sql & "D.strNiveau3 = '" & CBniv3.Text & "' AND "
            End If
            If CKBniv4.Checked Then
                sql = sql & "D.strNiveau4 = '" & CBniv4.Text & "' AND "
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End If
    If sql.EndsWith(" AND ") Then
        sql = sql.Substring(0, sql.Length - 4)
    End If
    ' Remove the WHERE if no textbox has been filled....'
    If sql.EndsWith(" WHERE ") Then
        sql = sql.Substring(0, sql.Length - 7)
    End If

    ' cmd = New OleDbCommand(sql, myConnection)
    cmd.CommandText = sql
    cmd.Connection = myConnection
    Dim MyDataSet As New DataSet
    da = New OleDbDataAdapter(sql, myConnection)
    da.SelectCommand = cmd

    da.SelectCommand.Parameters.Add(New OleDbParameter("@DatPrepa", DTPechprep.Value))  'adding date parameters to datatable
    da.SelectCommand.Parameters.Add(New OleDbParameter("@datau", DTPechprep.Value))     'adding date parameters to datatable
    'da.SelectCommand.Parameters.Add(New OleDbParameter("@Matrice", matrice.ToString))     'adding Matrice parameters to datatable

    da.Fill(MyDataSet, "Matrice")
    DataGridView1.DataSource = MyDataSet.Tables("Matrice")

    'to focus on first row of DGV after populating it 
    DataGridView1.Rows(0).Selected = True

    LBnumresult.Text = DataGridView1.RowCount - 1

2 个答案:

答案 0 :(得分:1)

这是我使用的答案,它包含3个表和左连接之间的2个内连接。 我先做内连接,最后是左连接

Dim sql =“SELECT distinct A. * FROM((tblMatrice AS B INNER JOIN tblRefMatrice AS C ON B.strMatrice = C.strMatrice)INNER JOIN tblRefMatDetails AS D ON C.intIDref = D.intIDRef)LEFT OUTER JOIN tblDopages作为A A.strMatrice = B.strMatrice,其中“

答案 1 :(得分:0)

你有一些语法错误。改变这个:

value = unicode(value, "utf-8")

到此:

SELECT * 
FROM (tblDopages AS A 
INNER JOIN tblMatrice AS B ON A.strMatrice = B.strMatrice) 
INNER JOIN (tblRefMatrice AS C ON B.strMatrice = C.strMatrice) 
INNER JOIN (tblRefMatDetails AS D ON C.intIDref = D.intIDRef) 
WHERE strMethode = 'MarMeth1' AND strPurif = 'MarPurif2'

一般:

SELECT * 
FROM ((tblDopages AS A 
INNER JOIN tblMatrice AS B ON A.strMatrice = B.strMatrice) 
INNER JOIN tblRefMatrice AS C ON B.strMatrice = C.strMatrice) 
INNER JOIN tblRefMatDetails AS D ON C.intIDref = D.intIDRef
WHERE strMethode = 'MarMeth1' AND strPurif = 'MarPurif2'