Writing the results of a query to a file
Messages   Related Types
This message was discovered on ASPFriends.com 'ngfx-io' list.


Josh Peters
-- Moved from [aspngdatagridrepeaterdatalist] to [ngfx-io] by Marcie Jones <Click here to reveal e-mail address> --

I currently have a dynamic query builder in ASP that the user can display
the results of the query in a table or dump to a double-quote qualified,
coma-delimited file. (to save as a .csv or whatever)

I'm in the process of rebuilding the application in .NET, but have not be
able to figure out how to write to the file in this mannor. Before it seemed
quite simple using set and looping through all the entries writing a line at
a time, but how do you do this in .NET. Below I have my code, which is very
similar to how I populate my datagrid for the same query, but in this
instance would like to write to the file.

Here's what I have and I 'm missing the code that makes this whole thing
work. Any help would be appriciated!!

Thanks Josh

Dim Query

Dim conn As SqlConnection

Dim cmd As SqlCommand

Dim objFSO, objTextFile

objFSO = CreateObject("Scripting.FileSystemObject")

objTextFile = objFSO.CreateTextFile(Server.MapPath("file1.txt")) ' Create
the file

Query = TextBox2.Text ' The query equals the value the user created in
Textbox2

cmd = New SqlCommand(Query)

conn = SQLConnect() ' Call SqlConnect to Create the connection

conn.Open() ' Open The Connection

cmd.Connection = conn ' Set the Connection Property

**************************************

WHAT GOES HERE?!?

**************************************

' Set the DataSource ?

objTextFile.writeline(cmd.ExecuteReader()) ' write to the file ?

' Bind the data to the ?

conn.Close()

Reply to this message...
 
    
Ryan Trudelle-Schwarz
Something like (this is OTTOMH so no guarantees):
-----------------------------------------------
System.Data.SqlClient.SqlDataReader
reader =3D cmd.ExecuteReader();

System.IO.FileStream
file =3D System.IO.File.Open("MyFile.csv", System.IO.FileMode.Create);

while(reader.Read())
{
for(System.Int32 i =3D 0; i < reader.FieldCount - 1; i++)
{
file.Write(reader[i].ToString() + ",");
}
file.Write(reader[reader.FieldCount - 1].ToString() + "\n");
}
-----------------------------------------------

hth, Ryan

-> -----Original Message-----
-> From: Josh Peters [mailto:Click here to reveal e-mail address]
->=20
-> I currently have a dynamic query builder in ASP that the user can =
display
-> the results of the query in a table or dump to a double-quote =
qualified,
-> coma-delimited file. (to save as a .csv or whatever)
->=20
-> I'm in the process of rebuilding the application in .NET, but have =
not be
-> able to figure out how to write to the file in this mannor. Before it
-> seemed
-> quite simple using set and looping through all the entries writing a =
line
-> at
-> a time, but how do you do this in .NET. Below I have my code, which =
is
-> very
-> similar to how I populate my datagrid for the same query, but in this
-> instance would like to write to the file.
->=20
-> Here's what I have and I 'm missing the code that makes this whole =
thing
-> work. Any help would be appriciated!!
->=20
-> Thanks Josh
->=20
-> Dim Query
->=20
-> Dim conn As SqlConnection
->=20
-> Dim cmd As SqlCommand
->=20
-> Dim objFSO, objTextFile
->=20
-> objFSO =3D CreateObject("Scripting.FileSystemObject")
->=20
-> objTextFile =3D objFSO.CreateTextFile(Server.MapPath("file1.txt")) ' =
Create
-> the file
->=20
-> Query =3D TextBox2.Text ' The query equals the value the user =
created in
-> Textbox2
->=20
->=20
->=20
->=20
-> cmd =3D New SqlCommand(Query)
->=20
->=20
-> conn =3D SQLConnect() ' Call SqlConnect to Create the connection
->=20
-> conn.Open() ' Open The Connection
->=20
-> cmd.Connection =3D conn ' Set the Connection Property
->=20
-> **************************************
->=20
-> WHAT GOES HERE?!?
->=20
-> **************************************
->=20
-> ' Set the DataSource ?
->=20
-> objTextFile.writeline(cmd.ExecuteReader()) ' write to the file ?
->=20
-> ' Bind the data to the ?
->=20
-> conn.Close()

---
[This E-mail scanned for viruses by Declude Virus]

Reply to this message...
 
 
System.Data.SqlClient.SqlCommand
System.Data.SqlClient.SqlConnection
System.Data.SqlClient.SqlDataReader
System.Int32
System.IO.File
System.IO.FileMode
System.IO.FileStream




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com