Where子句不处理SQLite Join请求

时间:2016-10-19 10:00:21

标签: vb.net sqlite

我需要ActPartB表上的ParteB字段的所有唯一值与userActTable匹配相同的IDNumber但仅适用于某些userIDNumber的条件(在userActTable上)

带有“WHERE”的完整sql字符串

sql = "SELECT DISTINCT ParteB FROM ActParteB INNER JOIN userAct ON IDNumber = IDNumber WHERE userAct.userIDNumber = ? order by ParteB asc"

然后我添加

cmdConnection.Parameters.AddWithValue("@userIDNumber", NumberOfActiveuser)

程序运行时没有调试错误,但它显示了ActPartB的所有唯一字段,无论userIDNumber的值如何。 os userIDNumber值很好,因为。

只是WHERE被“忽略”

sql = "SELECT DISTINCT ParteB FROM ActParteB INNER JOIN userAct ON IDNumber = IDNumber WHERE userAct.userIDNumber = ? order by ParteB asc"

产生与此相同的输出

sql = "SELECT DISTINCT ParteB FROM ActParteB INNER JOIN userAct ON IDNumber = IDNumber order by ParteB asc"

Where子句有什么问题?

完整代码:

Using conn As New SQLiteConnection(SQLiteConnStr)

Try
    conn.Open()

    Dim cmdConnection As SQLiteCommand = New SQLiteCommand(sql, conn)

    Dim sql = "SELECT DISTINCT ParteB FROM ActParteB INNER JOIN userAct ON IDNumber = IDNumber WHERE userAct.userIDNumber = ? order by ParteB asc"

            cmdConnection.Parameters.AddWithValue("@userIDNumber", NumberOfActiveUser)

            Dim readerParteB As SQLiteDataReader = cmdConnection.ExecuteReader()

            ParteBComboBox.Items.Clear()

            Try
                While (readerParteB.Read())
                    ParteBComboBox.Items.Add(readerParteB("ParteB"))
                End While
            Catch ex As Exception
                MsgBox(ex.ToString())
            End Try

            Catch ex As Exception
                MsgBox(ex.ToString())
            End Try

End Using

1 个答案:

答案 0 :(得分:2)

Dim cmdConnection As SQLiteCommand = New SQLiteCommand(sql, conn)

Dim sql = "..."

您的命令无法正常运行,因为它使用了不同的SQL字符串(您在代码的某些部分中声明的其他sql变量的值)。

此外,正如Jinx88909所述,您必须将表名添加到联接中以指定您所指的IDNumber列(如果有疑问,数据库当然会选择错误的列):

... JOIN userAct ON userAct.IDNumber = ActParteB.IDNumber ...
                    ^^^^^^^^           ^^^^^^^^^^

或者,当列名相同时,最好使用USING,它会自动从两个表中获取列:

... JOIN userAct USING (IDNumber) ...