Solved

Populate VB.net variables from SQL Select Query

Posted on 2013-11-27
3
739 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
  • 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 500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

803 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