0

我有一个看起来像这样的 SQL 查询;

Dim query As String = "SELECT ID, " & Environment.NewLine & _
                      "month_of_sale, " & Environment.NewLine & _
                      "client_last_name, " & Environment.NewLine & _
                      "client_first_name, " & Environment.NewLine & _
                      "deal_number, " & Environment.NewLine & _
                      "stock_number, " & Environment.NewLine & _
                      "delivery_date, " & Environment.NewLine & _
                      "finance_manager, " & Environment.NewLine & _
                      "finance_income, " & Environment.NewLine & _
                      "record_entered " & Environment.NewLine & _
                      "FROM(dodge_records)" & Environment.NewLine & _
                      "WHERE month_of_sale = @month_of_sale AND client_last_name = @client_last_name" & Environment.NewLine & _
                      "ORDER BY client_last_name"

我试图在 SQL 查询中插入一个变量,但在调试网站时没有看到它发生。这是其余的代码,以便更有意义。

Case "Last Name"
                    Try
                        Dim query As String = "SELECT ID, " & Environment.NewLine & _
                            "month_of_sale, " & Environment.NewLine & _
                            "client_last_name, " & Environment.NewLine & _
                            "client_first_name, " & Environment.NewLine & _
                            "deal_number, " & Environment.NewLine & _
                            "stock_number, " & Environment.NewLine & _
                            "delivery_date, " & Environment.NewLine & _
                            "finance_manager, " & Environment.NewLine & _
                            "finance_income, " & Environment.NewLine & _
                            "record_entered " & Environment.NewLine & _
                            "FROM(dodge_records)" & Environment.NewLine & _
                            "WHERE month_of_sale = @month_of_sale" & Environment.NewLine & _
                            "WHERE client_last_name = @client_last_name" & Environment.NewLine & _
                            "ORDER BY client_last_name"

                        Using Connection = New MySqlConnection(connStr)
                            Using da = New MySqlDataAdapter(query, Connection)
                                da.SelectCommand.Parameters.AddWithValue("@month_of_sale", CurrentMonth)
                                da.SelectCommand.Parameters.AddWithValue("@client_last_name", ValueBoxText)
                                Dim DS As New DataSet
                                da.Fill(DS)
                                DodgeSearchGridView.DataSource = DS
                                DodgeSearchGridView.DataBind()
                                Connection.Close()
                            End Using
                        End Using
                    Catch ex As Exception
                    End Try

CurrentMonth 和 ValueBoxText 是我需要插入到 SQL 查询中的字符串。现在这不是我想要的。我没有收到错误消息或任何内容,查询没有返回任何内容,并且我已经仔细检查了数据库以确保其中存在此​​查询将返回的数据。任何人都可以帮我检查代码,以便我了解如何使这项工作正常进行吗?谢谢!

4

2 回答 2

2

您的 SQL 不正确。您已经使用了 WHERE 两次。第二个条件应该使用 AND 即
AND client_last_name

于 2013-07-09T17:54:28.253 回答
1

单步调试 VS 调试器中的代码并将从命令生成的查询粘贴到 SQL Server Management Studio 中并在那里尝试。

于 2013-07-09T18:06:56.303 回答