views:

94

answers:

1

Please help me to read just one record from MS SQL table.
I tried to do that in the next way (IronPython 2.6 RC1):

cmd = SqlCommand("SELECT * FROM myTable", cn)  
dr = cmd.ExecuteReader()

After that I have ALL the table in dr! But need only ONE record (more precise: read table records one by one)
...
Sorry!
I was wrong!
I forget about two more commands in my program:
table=[]
for row in dr: table.append(row)
So it turned out an equivalent to dr.Read()!...

+1  A: 

Use TOP to restrict to one (random) record.

cmd = SqlCommand("SELECT TOP 1 * FROM myTable", cn)
dr=cmd.ExecuteReader()

Usually though when one is interested in a record, is interested in a specific record, like for example the one record with ID = 42.

TOP 1 will return the first record in the order the engine will choose to access the table.

Remus Rusanu
But how can I read all my table records one by one (in a cycle)? (The table is very big and there is not enough RAM for all the table)
Vladimir Aks
Why do you read them? I mean, what do you do with all those records you read? The SqlDataReader returned by ExecuteReader does *not* read all the records, it read one record at a time and you can cycle through all the records by caling SqlDataReader.Read() until all records are read, processing only one record at a time.
Remus Rusanu
"The SqlDataReader returned by ExecuteReader does not read all the records" - of course!When SqlDataReader is used in C# the above statement is correctand SqlDataReader.Read() is needed to cycle through the table. But when I tried to use SQLReader in IronPython I found that after executing dr=cmd.ExecuteReader() I have all the table in dr!!!It is strange to me, may be it is because of I am niewbie for IronPython?
Vladimir Aks
use the above solution by just adding a orderby field which is unique, so u can get one unique record at a time ordered in your desired way, I hope that solves your problem .. :)
Mahesh Velaga