I am using [SDAC](http://www.devart.com/sdac/) components to query a SQL Server 2008 database. It has a `recordcount`property as all datasets do and it also has the `FetchAll` property (which I think it is called `packedrecords` on `clientdatasets`). Said that, I got a few questions:

1 - If I set `FetchAll = True` the `recordcount` property returns ok. But in this case, when I have a large database and my query returns a lot of lines, sometimes the memory grows a lot (because it is fetching all data to get the `recordcount` of course).

2 - If I set `FetchAll = False`, the `recordcount` returns -1 and the memory does not grow. But I really need the recordcount. And I also wanna create a generic function for this, so I dont have to change all my existent queries.

What can I do to have the `recordcount` working and the memory usage of the application low in this case? 

Please, do not post that I dont need recordcount (or that I should use EOF and BOF) because I really do and this is not the question.

I thought about using a query to determine the recordcount, but it has some problems since my query is going to be executed twice (1 for recordcount, 1 for data)

**EDIT**

@Johan pointed out a good solution, and it seems to work. Can anybody confirm this? I am using 1 `TMSCconnection` for every `TMSQuery` (because i am using threads), so I dont think this will be a problem, will it?

```
  MSQuery1.FetchAll := False;
  MSQuery1.FetchRows := 10;
  MSQuery1.SQL.Text := 'select * from cidade';
  MSQuery1.Open;
  ShowMessage(IntToStr(MSQuery1.RecordCount)); //returns 10
  MSQuery1.Close;

  MSQuery2.SQL.Text := 'SELECT @@rowcount AS num_of_rows';
  MSQuery2.Open;
  ShowMessage(MSQuery2.FieldByName('num_of_rows').AsString); //returns 289

```

**EDIT 2\***

`MSQuery1` must be closed, or `MSQuery2` will not return the `num_of_rows`. Why is that?

```
  MSQuery1.FetchAll := False;
  MSQuery1.FetchRows := 10;
  MSQuery1.SQL.Text := 'select * from cidade';
  MSQuery1.Open;
  ShowMessage(IntToStr(MSQuery1.RecordCount)); //returns 10
  //MSQuery1.Close; <<commented

  MSQuery2.SQL.Text := 'SELECT @@rowcount AS num_of_rows';
  MSQuery2.Open;
  ShowMessage(MSQuery2.FieldByName('num_of_rows').AsString); //returns 0

```
['https://Stackoverflow.com/questions/6284893', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/528211/']
This code:

```
StreamReader sourceStream = new StreamReader(filePath +"\\"+ fileName);
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

```

You're reading the bytestream as text with a specific encoding (UTF8)... but GIF and ZIP are binary files, not text files. The encoding is mangling them.

Try using something like [ReadAllBytes](http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes.aspx):

```
byte[] fileContents = File.ReadAllBytes("filepath");

```
You are reading binary data to string (assuming it utf8) and converts it back to bytes array. That's completely wrong.