473,795 Members | 3,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.te xt = Cstr(texthold)
conn.Connection String = _
System.Configur ation.Configura tionSettings.Ap pSettings.Get _
("ConnectionStr ing")
conn.Open()
Dim querydb As New OleDbCommand("S ELECT * FROM tapeinfo
WHERE LIKE texthold", conn)
Dim results As OleDbDataReader
Dim i As Integer
results = querydb.Execute Reader(CommandB ehavior.CloseCo nnection)
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 2018
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(my Query, 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.te xt = Cstr(texthold)
conn.Connection String = _
System.Configur ation.Configura tionSettings.Ap pSettings.Get _
("ConnectionStr ing")
conn.Open()
Dim querydb As New OleDbCommand("S ELECT * FROM tapeinfo
WHERE LIKE texthold", conn)
Dim results As OleDbDataReader
Dim i As Integer
results = querydb.Execute Reader(CommandB ehavior.CloseCo nnection)
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.Sql Client.SqlConne ction

Dim cmd As System.Data.Sql Client.SqlComma nd = conn.CreateComm and

cmd.CommandType = CommandType.Sto redProcedure

cmd.CommandText = "YourProcedureN ame"

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

Dim reader As System.Data.Sql Client.SqlDataR eader

reader = cmd.ExecuteRead er

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.te xt = Cstr(texthold)
conn.Connection String = _
System.Configur ation.Configura tionSettings.Ap pSettings.Get _
("ConnectionStr ing")
conn.Open()
Dim querydb As New OleDbCommand("S ELECT * FROM tapeinfo
WHERE LIKE texthold", conn)
Dim results As OleDbDataReader
Dim i As Integer
results = querydb.Execute Reader(CommandB ehavior.CloseCo nnection)
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
7561
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 of the proper command line syntax and the code to set the variable in the aspx program. thanks
8
2616
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 to run from VB. My pass-thru query retrieves data from our AS/400 that I use to build a local table (on my PC). My pass-thru and local do in fact work together when I run them interactively. But I want, no make that NEED, to run them from VB. ...
7
21637
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 server ? Is it possible to use parameters in pass-through queries ? An additional question: Is it possible to connect to a database on MySQL or PostgreSQL using ADO ? Is it possible to execute pass-through queries with parameters, using ADO...
10
3945
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 my method. How can I do this?
2
1952
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 needs to pass a Date data-type that is empty much like a string would be empty if we were to place double quotes as in "". The reason it needs to be empty is because I am passing parameters to a
2
19795
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 <selectParameters> but i don't know which elements are needed and how... This is my aspx file: <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\mydb.mdb"
5
5750
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 history from elementary to phd and on to post doctoral work alll on the same row. I want to break that up to have each level of education as a seperate record and then relate that back to the person. i can pull the data into a recordset I can...
12
11115
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. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
5
7418
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
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10437
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10214
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.