|
-
Dec 29th, 2010, 10:57 AM
#1
Thread Starter
New Member
Build query based on two variables
Hi All,
In my program I have a combo box that the user selects a date from and a list view that the user selects an employee ID from. How do I properly design my database query string to use both the date selected and the employee ID selected? The date is stored in a variable called YearMonth and the employee ID is stored in a variable called EmpID.
Here is what I am trying to do with my query:
Code:
Dim ExactQuery As String = _
"SELECT * " & _
"FROM[JobTypeExact_Query] WHERE (EMPID = ?) AND WHERE (YEARMONTH = YM)" 'Define Exact job type query
ExactQuery = ExactQuery.Replace("?", CStr(EmpID)).Replace("YM", CStr(YearMonth)) 'Constrain to Employee and YearMonth chosen
Is there something wrong with my syntax here and/or is there an easier way to accomplish this?
Thanks in advance for your help,
Diana
-
Dec 29th, 2010, 11:23 AM
#2
Re: Build query based on two variables
-
Dec 29th, 2010, 11:24 AM
#3
Addicted Member
Re: Build query based on two variables
y dont u just put the variables in when declaring? y replace?
Dim ExactQuery As String = _
"SELECT * " & _
"FROM[JobTypeExact_Query] WHERE (EMPID = EmpID) AND WHERE (YEARMONTH = YearMonth)"
-
Dec 29th, 2010, 11:26 AM
#4
Addicted Member
Re: Build query based on two variables
i meant
Dim ExactQuery As String = _
"SELECT * " & _
"FROM[JobTypeExact_Query] WHERE (EMPID = " & EmpID & ") AND WHERE (YEARMONTH = "& YearMonth & ")" 'Define Exact job type query
-
Dec 29th, 2010, 12:04 PM
#5
Re: Build query based on two variables
because the query would still be wrong... and it's a bad habit.
-tg
-
Dec 29th, 2010, 01:02 PM
#6
Thread Starter
New Member
Re: Build query based on two variables
Well,
That was a lot to read and I am still confused and not sure how to implement using parameters in my query at all. It seems that I would also have to change a lot more than just my query.
Can someone please be more helpful than telling me to read an article?
Thanks!
-Diana
-
Dec 29th, 2010, 02:13 PM
#7
Re: Build query based on two variables
First, in all SQL queries, you only need one WHERE clause
Code:
'it would not be
"FROM[JobTypeExact_Query] WHERE (EMPID = ?) AND WHERE (YEARMONTH = YM)"
'but rather
"FROM[JobTypeExact_Query] WHERE (EMPID = ?) AND YEARMONTH = YM"
With that in mind, lets add some parameters. Try this:
Code:
Dim ExactQuery As String = _
"SELECT * " & _
"FROM[JobTypeExact_Query] WHERE EMPID = @EMPID AND YEARMONTH = @YM"
Dim command As New SqlCommand(ExactQuery, myConnection) 'here replace myConnection
'with your connection object
command.Parameters.AddWithValue("@EMPID", YourListViewName)
command.Parameters.AddWithValue("@YM", YourComboBoxName)
That is the basics of using parametized queries.
<Pet_Peeve>Do you really need to have every single solitary field in the entire table returned for your query to work? If not, then why are you doiing SELECT *? Your SELECT clause should contain only those fields that you need for that particular query.</Pet_Peeve>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|