Question How do I use variables in a SQL statement

tgf-47

Member
Joined
Feb 16, 2010
Messages
15
Programming Experience
Beginner
I got this in my SQL statement. I "BOLDED" the problem.

VB.NET:
SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate, ShippedDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, ShipRegion, ShipPostalCode, ShipCountry FROM Orders 
WHERE CustomerID = [B]@CustomerID[/B]

I saw a video tutorial on the internet, How Do I: Use DataSets in an N-Tier Application? where they are doing this.
The example that I inserted is actually from the tutoral. For some reason it wont work on my side.

How do I use variables in a SQL statement?
 
You would use a variable created on your connection object. With that you can use parameters.

I don't know what you are using for a database, but lets pretend it is SQL Server. You would do something like:
VB.NET:
Dim sSQL As String
sSQL = "SELECT OrderID, CustomerID, EmployeeID, OrderDate, RequiredDate, 
sSQL = sSQL & "ShippedDate, ShipVia, Freight, ShipName, ShipAddress, ShipCity, 
sSQL = sSQL & "ShipRegion, ShipPostalCode, ShipCountry "
sSQL = sSQL & "FROM Orders "
sSQL = sSQL & "WHERE CustomerID = @CustomerID "
Dim command As New SqlCommand(sSQL, yourConnectionObject)
Command.Parameters.AddWithValue("@CustomerID", txtCustomerID.Text)
It is such a pleasure to see young programmers using SQL. Keep up the good work! :)
 

Latest posts

Back
Top