473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

How to pass a variable to a SQL command

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.AppSett ings.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.CloseConnect ion)
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 =---
Nov 18 '05 #1
2 2005
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

Quarantine wrote:
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.AppSett ings.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.CloseConnect ion)
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 =---


Nov 18 '05 #2
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" <br*************@comcast-dot-net.no-spam.invalid> wrote in
message news:40********@127.0.0.1...
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.AppSett ings.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.CloseConnect ion)
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

Nov 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mike Ratliff | last post by:
I need to call internet explorer from another program and pass along a variable to the aspx page that I am opening. example iexplorer.exe "search.aspx?variable1="12345" please give me an example...
8
by: Tcs | last post by:
I've been stumped on this for quite a while. I don't know if it's so simple that I just can't see it, or it's really possible. (Obviously, I HOPE it IS possible.) I'm trying to get my queries...
7
by: Zlatko Matiæ | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
10
by: John Bailo | last post by:
I want to pass a SqlCommand object as a input parameter to a method. I want to pass the SqlCommand "by value" so that any updates to the original object are *not* reflected in the object within...
2
by: glenn | last post by:
Hi folks, I guess this question has to do with formatting dates. Seems like it should be a no brainer but have spent too much time hunting so I am posting the forum. I have a function that...
2
by: phil | last post by:
Hi, I want to put a recordset in a gridview but i don't know how to pass the value of the variable in the 'where' statement. The value of the variable is set in the code-behind. i added a tag...
5
by: techbuddha | last post by:
Hi new to the forum and visual basic. I am attempting to fix a migration of excel to access. The excel sheets where simple copied as is into access. for example one table lists the academic...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
5
dlite922
by: dlite922 | last post by:
All i want to do in C++ is pass a few arguments, concatenate them into variable and call system() I've gotten this far: #include <stdio.h> #include <stdlib.h> #include <string>
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.