473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

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 indeed.

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

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

Jul 7 '06 #1
4 9935
All you have to do is use a SqlCommand set up to call this stored
procedure and then get a SqlDataReader or a fill a DataSet using a
SqlDataAdapter. With the reader, you will probably use less resources, with
the DataSet, you get the advantage of having the whole thing in memory.

Regardless, once you get either of these, you will iterate through them
(the SqlDataReader, or the DataSet), and just write your result set out row
by row. You will only have one column in each, and they will be in string
format. You just have to pass the string in each row to the Write method.
It should handle the encoding for you.

If you need a specific encoding, then you will have to do handle that
yourself, but it isn't too hard, using the Encoding class (combined with the
BinaryWrite method on the HttpResponse).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Fr*************@Hotmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
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 indeed.

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

...
...
...

Jul 7 '06 #2
Hi Nicholas,

Many thanks for your reply.

I did try something like this without success, but I think that the
encoding that you mentioned could be the crucial bit, so I'll try that.

I know that with the old Classic ASP, it only worked when the
ADODB.Stream object's type was set to 1 (binary).

Thanks again for your prompt reply.

Best regards
David

Nicholas Paldino [.NET/C# MVP] wrote:
All you have to do is use a SqlCommand set up to call this stored
procedure and then get a SqlDataReader or a fill a DataSet using a
SqlDataAdapter. With the reader, you will probably use less resources, with
the DataSet, you get the advantage of having the whole thing in memory.

Regardless, once you get either of these, you will iterate through them
(the SqlDataReader, or the DataSet), and just write your result set out row
by row. You will only have one column in each, and they will be in string
format. You just have to pass the string in each row to the Write method.
It should handle the encoding for you.

If you need a specific encoding, then you will have to do handle that
yourself, but it isn't too hard, using the Encoding class (combined with the
BinaryWrite method on the HttpResponse).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Fr*************@Hotmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
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 indeed.

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

...
...
...
Jul 8 '06 #3
David,

It worked not only because you set the type to 1, but because you set
the character set as well. This way, ADO knew how to encode the text to the
stream that you were outputting. And then you did a raw dump of those bytes
to the result stream.

You just have to do some work in encoding the text in the
dataset/datareader and you should be fine.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Fr*************@Hotmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
Hi Nicholas,

Many thanks for your reply.

I did try something like this without success, but I think that the
encoding that you mentioned could be the crucial bit, so I'll try that.

I know that with the old Classic ASP, it only worked when the
ADODB.Stream object's type was set to 1 (binary).

Thanks again for your prompt reply.

Best regards
David

Nicholas Paldino [.NET/C# MVP] wrote:
>All you have to do is use a SqlCommand set up to call this stored
procedure and then get a SqlDataReader or a fill a DataSet using a
SqlDataAdapter. With the reader, you will probably use less resources,
with
the DataSet, you get the advantage of having the whole thing in memory.

Regardless, once you get either of these, you will iterate through
them
(the SqlDataReader, or the DataSet), and just write your result set out
row
by row. You will only have one column in each, and they will be in
string
format. You just have to pass the string in each row to the Write
method.
It should handle the encoding for you.

If you need a specific encoding, then you will have to do handle that
yourself, but it isn't too hard, using the Encoding class (combined with
the
BinaryWrite method on the HttpResponse).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Fr*************@Hotmail.comwrote in message
news:11*********************@m73g2000cwd.googlegr oups.com...
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 indeed.

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

...
...
...

Jul 8 '06 #4
Hi Nicholas,

I followed your advice and it worked a treat.

I used SqlDataReader because forward only scrolling was fine for this
scenario and it uses less resources.

I didn't actually do anything to change the encoding of the text and it
still worked fine.

I'm really pleased that I've managed to implement this technique (with
your help) in C# 2005 as it is very useful.

Many thanks again to you for such a prompt and intelligent reply.

Best regards
David
Nicholas Paldino [.NET/C# MVP] wrote:
David,

It worked not only because you set the type to 1, but because you set
the character set as well. This way, ADO knew how to encode the text to the
stream that you were outputting. And then you did a raw dump of those bytes
to the result stream.

You just have to do some work in encoding the text in the
dataset/datareader and you should be fine.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Fr*************@Hotmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
Hi Nicholas,

Many thanks for your reply.

I did try something like this without success, but I think that the
encoding that you mentioned could be the crucial bit, so I'll try that.

I know that with the old Classic ASP, it only worked when the
ADODB.Stream object's type was set to 1 (binary).

Thanks again for your prompt reply.

Best regards
David

Nicholas Paldino [.NET/C# MVP] wrote:
All you have to do is use a SqlCommand set up to call this stored
procedure and then get a SqlDataReader or a fill a DataSet using a
SqlDataAdapter. With the reader, you will probably use less resources,
with
the DataSet, you get the advantage of having the whole thing in memory.

Regardless, once you get either of these, you will iterate through
them
(the SqlDataReader, or the DataSet), and just write your result set out
row
by row. You will only have one column in each, and they will be in
string
format. You just have to pass the string in each row to the Write
method.
It should handle the encoding for you.

If you need a specific encoding, then you will have to do handle that
yourself, but it isn't too hard, using the Encoding class (combined with
the
BinaryWrite method on the HttpResponse).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<Fr*************@Hotmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
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 indeed.

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

...
...
...
Jul 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Richard K Bethell | last post by:
Hi, We have an application, written in ASP, that uses the ADODB.Stream to be able to open files and write byte arrays to the Response object. If one of our administrators patches the web servers...
3
by: Marty | last post by:
Hi, What is the VB.NET equivalent of the VB6 ADODB.Connector and ADODB.Recordset? Thanks Marty
1
by: guyv | last post by:
Hello! I wanna read binary data from database with ADODB.Stream object. So I wrote code.. --------- <% query = "SELECT * FROM Categories" adoDB.DefaultDatabase = "Northwind"
0
by: Brendan Reynolds | last post by:
I have a web service that takes the XML representation of a dataset and uses XSLT to transform it to the format required to open a disconnected ADODB recordset on the XML, and returns the...
3
by: C | last post by:
Re: Microsoft Knowledge Base Article - 870669 How to disable the ADODB.Stream object from Internet Explorer You may recently have heard of the vulnarability exposed by Internet Explorer as...
0
by: Fresh_Air_Rider | last post by:
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....
7
by: iporter | last post by:
I use the code below to authorise the download of certain files. Thus, instead of linking to the file in a wwwroot directory, I link to this code with the filename as a parameter, and the script...
7
by: Bryan | last post by:
Hi , I am using ADO (ADODB) with access database. Not sure what I am doing wrong.here. Can anyone please help me? string mdbFile = System.IO.Directory.GetCurrentDirectory() +" \\bTrack.mdb;"...
10
by: =?Utf-8?B?SnVhbg==?= | last post by:
Hi! I want to use ASP to download big files using ADODB.STREAM. It works very fine with files smaller than 80 MB. On the Webserver I can see that memory allocation and the process w3wp is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.