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

x
?
Solved

Best way to get a single row

Posted on 2004-08-08
5
Medium Priority
?
249 Views
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.
0
Comment
Question by:kplonk
5 Comments
 
LVL 37

Accepted Solution

by:
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.
0
 
LVL 15

Expert Comment

by:Timbo87
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;
connectionObject.Open();
SqlDataReader dr = cmd.ExecuteReader();
dr.Read();
col1 = dr.GetString(0);
col2 = dr.GetString(1);
col3 = dr.GetString(2);
dr.Close();
connectionObject.Close();
0
 

Author Comment

by:kplonk
ID: 11748180
Thanks just what i wanted
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 11748227
Anytime. Glad to help.

Cheers,

Greg
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

916 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