ADODB.Stream equivalent in C# for retreaving records in CSV format

  • Thread starter Fresh_Air_Rider
  • Start date
F

Fresh_Air_Rider

Hi

In the "good old" Classic ASP days, I used to stream records from a SQL
Server database out to the user's browser in CSV format by using a
combination of COALESCE and the ADODB.Stream object.

The trouble is that I'm not really sure how to do this in C# 2005.

This was a very useful technique and if anyone could please show me how
to adapt the Classic ASP code to C# 2005, I'd be very grateful.

The old Classic ASP method went something like the following and
setting the ADODB.Stream object's type to 1 (binary) was crucial

SQL Server Stored Procedure
-------------------------------------------

DECLARE @CRLF AS CHAR(2)
DECLARE @Delimiter AS CHAR(1)

SET @CRLF = CHAR(13) + CHAR(10)
SET @Delimiter = ','

SELECT
Title + @Delimiter +
Forename + @Delimiter +
Surname + @CRLF
FROM Users

Classic ASP code
---------------------------

Set oADOConn = Server.CreateObject("ADODB.Connection")
Set oADOStream = Server.CreateObject("ADODB.Stream")
Set oADOCmd = Server.CreateObject("ADODB.Command")

oADOStream.Charset = "windows-1252"

oADOConn.Open Application("ConnStr")
oADOCmd.CommandType = adCmdStoredProc
oADOCmd.ActiveConnection = oADOConn
oADOCmd.CommandText = "GetUsersInCSVFormat"

oADOStream.Open
oADOCmd.Properties("Output Stream") = oADOStream
oADOCmd.Execute , , adExecuteStream

Set oADOCmd = Nothing
Set oADOConn = Nothing

Response.AddHeader
"content-disposition","attachment;filename=Users.csv"
Response.ContentType = "application/csv"

'Set type to binary
oADOStream.Type = 1

If oADOStream.Size = 0 Then
Response.Write "No records available"
Else
'Write out the binary data
Response.BinaryWrite oADOStream.Read
End If

....
....
....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top