Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I use a SqlDataReader to get two columns of data?

Posted on 2010-09-22
3
Medium Priority
?
262 Views
Last Modified: 2012-05-10
If I wanted the password associated with a userName, this would work:

cmd = new SqlCommand("Select pw from [userTable] where login='" + userName + "'", conn);

if (reader.Read())
{
    lookupPassword = (string)reader[0];
}

But what if I wanted the password and a second column of information?  Hw would I get this from the reader?  What if my query were:

cmd = new SqlCommand("Select pw, roleID from [userTable] where login='" + userName + "'", conn);


The first row of data would still be reader[0].  But how would I differentiate between the pw and roleID columns?

Thanks,
newbieweb
0
Comment
Question by:newbieweb
3 Comments
 
LVL 11

Accepted Solution

by:
Larissa T earned 1600 total points
ID: 33737673
I think this is
 lookupRole = (string)reader[1];

more details on sqlreader
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.aspx
0
 
LVL 13

Assisted Solution

by:Onthrax
Onthrax earned 400 total points
ID: 33738875
You can use the title of the column to fetch the column

e.g. reader("pw") and reader("roleID")

(vb style)

Hope this helps..
0
 

Author Closing Comment

by:newbieweb
ID: 33738885
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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Loops Section Overview

926 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