Best way to get a single row

Posted on 2004-08-08
Medium Priority
Last Modified: 2012-03-15
Hi, if I have a stored procedure that when executed will return one row, with three columns, what is the best way to get the data from this. At the mo I have a sqldataadapter filling a dataset, but ho do I get the data from there, I want three vars say v1,v2 and v3 to be populated with the string vals in the row?

Thanks Kieran.
Question by:kplonk
LVL 37

Accepted Solution

gregoryyoung earned 80 total points
ID: 11747590
DataAdapter.Fill(yourDataSet, "tablename");

if(yourDataSet.Tables["tablename"].Rows.Count == 1) {
    DataRow dr = yourDataSet.Tables["tablename"].Rows[0];
    //here you can access variables like dr["name"] ...

given that you only return 3 values you might want to look at output parameters from your stored procedure.
LVL 15

Expert Comment

ID: 11747733
Even a DataAdapter and DataSet may be a bit overkill. If you're just reading the data and not updating it, a DataReader should do the job.

string col1, col2, col3;
SqlCommand cmd = new SqlCommand("storedProcedure", connectionObject);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataReader dr = cmd.ExecuteReader();
col1 = dr.GetString(0);
col2 = dr.GetString(1);
col3 = dr.GetString(2);

Author Comment

ID: 11748180
Thanks just what i wanted
LVL 37

Expert Comment

ID: 11748227
Anytime. Glad to help.



Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

612 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