SQL Query problem

  • Thread starter Thread starter tiger79
  • Start date Start date
T

tiger79

Hi,
I've implemented the following code :

public int Count()

{

int count=0;

SqlCeConnection con = new SqlCeConnection(@"Data Source=\My
Documents\DICT.sdf");

con.Open();

string tSQL = "SELECT COUNT(*) FROM Entity";

SqlCeCommand cmdSelect = new SqlCeCommand(tSQL, con);

cmdSelect.CommandType = CommandType.Text;

SqlCeDataReader dtr = cmdSelect.ExecuteReader(CommandBehavior.Default);

count = dtr.GetInt32(0);

con.Close();

return count;

}



Ok, the code seems quite straight-forward to me. I got a Table (Entity) in a
database (DICT.sdf). Now I'd like to know how many rows are in this table.
So I use the COUNT aggregate function of SQL. The returned value I want to
place in the int variable count. I do so by making a datareader and reading
the value from there....

But i get the following error :

No data exist for the Row/Column

I dont really understand what's meant with that and how to get it to work...
Any ideas ???
 
Tiger:

Dan's way is the preferred way, but if you need to use a Reader in the
future, remember that you need to call .Read to advance the record and use a
loop to get through all of them... ie while(rdr.Read()){ //}

--

W.G. Ryan, eMVP

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
 
I've used a redaer in some other software where I (like you suggest) used a
loop, but I thought I didnt need one in this case cause I only had to read
one number (te return value of the count statement).
Thank you in any case...
btw, if I use Dan's method do I have to place it in a loop as well if I'd
like to read multiple sets of data ?
 
Back
Top