?
Solved

Populate VB.net variables from SQL Select Query

Posted on 2013-11-27
3
Medium Priority
?
777 Views
Last Modified: 2013-11-27
I am calling this sql command from vb.net:

cmd.CommandText = "SELECT * FROM SNUGZ_EXT00101_SOP10200_2 WHERE PT_UD_KEY = '" & strKey & "'"
       
The sql query will return 1 row with several columns labeled PT_UD_1, PT_UD_2, etc.

I want to put the values in the columns into variables in my vb.net code.  For example:

strImprint = PT_UD_1
strImprintColor = PT_UD_2


How do I code my vb.net to do this?
0
Comment
Question by:thoecherl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:thoecherl
ID: 39682181
Additional information:

I only want to run the SQL query once.  I don't want to run it each time for each of the 12 variables I need to populate.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 39682186
Use the ExecuteReader method of your command object.

e.g.

Dim reader As SqlDataReader = cmd.ExecuteReader()

If reader.Read() Then  ' If there is at least one row
    strImprint = reader("PT_UD_1").ToString()
    strImprintColor = reader("PT_UD_2").ToString()
End If

Open in new window


Also be aware that your query is susceptible to SQL Injection. Building SQL queries using string concatenation is a bad practice. I suggest reading up on parameterized queries.
0
 

Author Closing Comment

by:thoecherl
ID: 39682366
Did the trick.  Thanks.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question