简体   繁体   English

将变量插入SQL查询Visual Studio 2010中

[英]Insert Variable Into SQL Query Visual Studio 2010

I have a SQL query that looks like this; 我有一个看起来像这样的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"

I am trying to insert a variable into the SQL query but I don't see it happening when I debug the website. 我试图在SQL查询中插入变量,但是调试网站时却看不到它的发生。 Here is the rest of the code so that it makes more sense. 这是其余的代码,因此更有意义。

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 are strings that I need inserted into the SQL query. CurrentMonth和ValueBoxText是我需要插入到SQL查询中的字符串。 Right now this isn't working how I want it to. 现在,这不符合我的要求。 I don't get an error message or anything, the query doesn't return anything and I have double checked the database to make sure there is data in it that would be returned by this query. 我没有收到错误消息或任何内容,查询没有返回任何内容,并且我再次检查了数据库以确保其中包含该查询将返回的数据。 Can anyone help me error check the code so I can find out how to make this work? 任何人都可以帮助我对代码进行错误检查,以便我找出如何进行这项工作的人吗? Thanks! 谢谢!

Your SQL is incorrect. 您的SQL不正确。 You have used WHERE twice. 您已使用WHERE两次。 The second condition should use AND ie 第二个条件应该使用AND
AND client_last_name AND client_last_name

在VS调试器中单步执行代码,并将命令生成的查询粘贴到SQL Server Management Studio中,然后在其中进行尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM