I have to get a DataTable from SQL, and then convert it to XML (I can't use SQL XML) and save to a file. The problem is that the file is going to be 5 GB, and I don't have enough memory to download it all and convert it to XML all at once in memory. I know I should be able to use a DataReader to convert and push the data to the file stream in chunks, but I don't know how to really do this. How can I do the following?
- Connect to the SQL server asynchronously
- Call the stored procedure asynchronously
- Read data from the DataReader asynchronously
- Convert data from the DataReader to XML and save to file asynchronously
This all has to be asynchronous because it is on a server processing multiple requests and we can't have a thread blocking for each one.