Return one record from database

  • Thread starter Thread starter Mark Tait
  • Start date Start date
M

Mark Tait

Hi - I want to query a database to return just one record:

..select id, parentid, question, answer from tblquestions where id =
@id)..

read the fields returned from that record into variables:

id=rs("id").value
parentid=rs(parentid").value
...
..

..and close the connection.

I don't want to populate datasets etc - I've heard about the
executescalar and executeonerecordonly(or similar) - but haven't seen
any examples - can anyone please procvide an example of this for me?

Many thanks for any help,


Mark Tait
(e-mail address removed)
www.fixitks.co.uk
 
--sql 2000
select top 1 * from tablename
--sql 7
set rowcount 1
select * from tablename

executescalar returns the first column of the first row...
 
Hi - thankyou, don't think I made myself clear though.

I know about sql statements (top, limit etc), it's ASP.Net I'm getting
to grips with.

My sql statement knows the ID to select - I just don't know how to do
this efficiently from ASP.Net without getting the info into a dataset
first, and then binding it to something.

All I want is to select a record, and read the fields values in that
record into some variables, which I can then manipulate elsewhere. Can
I do this without having to read a dataset and bind it?

Thanks,

Mark Tait
(e-mail address removed)
www.fixitks.co.uk
 
All I want is to select a record, and read the fields values in that
record into some variables, which I can then manipulate elsewhere. Can
I do this without having to read a dataset and bind it?

string strCon = "Your connection string"
SqlDataReader dr;

dr = SqlHelper.ExecuteReader(strCon, CommandType.Text, "select blah blah
blah")

dr.Read()
dr.GetString(0)
....
dr.GetString(n)


There you go. Hope that helps. The GetStrings return the fields.

-- Rick
 
Back
Top