How to pass a variable to a SQL command

Q

Quarantine

I am still a noob at the whole .NET, but I have a question and hope
someone can help me out. I am taking user input, then want to query
a database and return the results to either a datagrid, or a
dynamically created table. I want to be able to pass a variable to
the SQL command that I have created, but I don't know how to get the
format right. Here is a snippet of my code I know that the format
of my SQL command is totally wrong after the WHERE portion. If
anybody could help me, I would appreciate it a lot!

Dim conn As OleDbConnection
Dim texthold as String
conn = New OleDbConnection
txtUserInput.text = Cstr(texthold)
conn.ConnectionString = _
System.Configuration.ConfigurationSettings.AppSettings.Get _
("ConnectionString")
conn.Open()
Dim querydb As New OleDbCommand("SELECT * FROM tapeinfo
WHERE LIKE texthold", conn)
Dim results As OleDbDataReader
Dim i As Integer
results = querydb.ExecuteReader(CommandBehavior.CloseConnection)


Bryant
 
M

MattB

I'm also pretty new to this, but I think I've stubbled over this one
already.
Build your query as a string and concatenate it together with your variables
like this:

dim myQuery as string
....
myQuery = "SELECT * FROM tapeinfo WHERE LIKE " & texthold
....
Dim querydb As New OleDbCommand(myQuery, conn)


Good luck!

Matt
 
N

noone

Dim conn As System.Data.SqlClient.SqlConnection

Dim cmd As System.Data.SqlClient.SqlCommand = conn.CreateCommand

cmd.CommandType = CommandType.StoredProcedure

cmd.CommandText = "YourProcedureName"

cmd.Parameters.Add("@yourParam", yourValue)

Dim reader As System.Data.SqlClient.SqlDataReader

reader = cmd.ExecuteReader



This is for SQL server, it is about the same for OleDbConnection

you create a command that is associated to your connection, add parmaers to
pass on to your stored procedures, execute the reader. this scheme is read
once, forward only. You can do what you want with the results, put them in a
dataset, just plug it into controls or whatever. If your using Visual Studio
it is a little easier just to drop things on the designer, then bind your
controls to the results. You can add parameters in the designer and even
generate Select, Delete Etc staments as procedures(Best Faster) or SQL
statements.


Quarantine said:
I am still a noob at the whole .NET, but I have a question and hope
someone can help me out. I am taking user input, then want to query
a database and return the results to either a datagrid, or a
dynamically created table. I want to be able to pass a variable to
the SQL command that I have created, but I don't know how to get the
format right. Here is a snippet of my code I know that the format
of my SQL command is totally wrong after the WHERE portion. If
anybody could help me, I would appreciate it a lot!

Dim conn As OleDbConnection
Dim texthold as String
conn = New OleDbConnection
txtUserInput.text = Cstr(texthold)
conn.ConnectionString = _
System.Configuration.ConfigurationSettings.AppSettings.Get _
("ConnectionString")
conn.Open()
Dim querydb As New OleDbCommand("SELECT * FROM tapeinfo
WHERE LIKE texthold", conn)
Dim results As OleDbDataReader
Dim i As Integer
results = querydb.ExecuteReader(CommandBehavior.CloseConnection)


Bryant



----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---



x-- 100 Proof News - http://www.100ProofNews.com
x-- 3,500+ Binary NewsGroups, and over 90,000 other groups
x-- Access to over 800 Gigs/Day - $8.95/Month
x-- UNLIMITED DOWNLOAD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top