Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 SQL DataReader and StreamWriter...

Author  Topic 

CorbanDavies
Starting Member

3 Posts

Posted - 2010-06-02 : 13:58:47
I need to create and write a CSV file from a SQL database based on query. The code below works, but the CSV result contains only the first column of data from the SQL source and no commas or other data values from the SQL connection.

Can anyone help?
------------------------------------------

Dim column_1 As Decimal
Dim column_2 As DateTime
Dim column_3 As String
Dim column_4 As Decimal

'Create a CSV file
Dim oWriter As System.IO.StreamWriter
oWriter = System.IO.File.CreateText("C:\...\Temp.csv")

'Connection String and SQLConnect
Dim connstring As String = "server=server;database=database;user id=user;password=password"
Dim conn As New SqlConnection(connstring)

'Create Selection Query
Dim selectSQL As String = "SELECT ACCT, DATE, DOW, KWH FROM SENSUS3 WHERE (ACCT = " & txtAcct.Text & ")"

'Create Selection Command
Dim cmd As New SqlCommand(selectSQL, conn)

'Set cmd Property
cmd.CommandType = CommandType.Text

'Open Database
conn.Open()

'Create a new Reader and execute the Reader
Dim myreader As SqlDataReader = cmd.ExecuteReader

'Read information from the database and give the values to the arguments(column_1 to column_4)
While myreader.Read
column_1 = myreader.GetDecimal(0)
column_2 = myreader.GetDateTime(1)
column_3 = myreader.GetString(2)
column_4 = myreader.GetDecimal(3)
oWriter.WriteLine(column_1, ",", column_2, ",", column_3, ",", column_4)
End While

oWriter.Dispose()
conn.Dispose()

------------------------------------------
Basically, this code creates the Temp.csv file, but it only contains one column of data: ACCT, and does not have any of the delimited columns to the right of the first. Any thoughts?

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-06-02 : 14:18:33
Change
oWriter.WriteLine(column_1, ",", column_2, ",", column_3, ",", column_4)

to
oWriter.WriteLine(column_1 & "," & column_2 & "," & column_3 & "," & column_4)
Go to Top of Page

CorbanDavies
Starting Member

3 Posts

Posted - 2010-06-02 : 14:27:17
quote:
Originally posted by vijayisonly

Change
oWriter.WriteLine(column_1, ",", column_2, ",", column_3, ",", column_4)

to
oWriter.WriteLine(column_1 & "," & column_2 & "," & column_3 & "," & column_4)




That worked perfectly. Thanks.
Go to Top of Page

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2010-06-02 : 14:28:31
Np. Hope you figured the reason too
Go to Top of Page

CorbanDavies
Starting Member

3 Posts

Posted - 2010-06-02 : 15:03:30
Yep... It seems so obvious when someone else points it out.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-06-02 : 16:27:13
OR something like

bcp "SELECT ACCT+','+DATE+','+DOW+','+KWH FROM db.owner.SENSUS3 WHERE ACCT = 'x'" QUERYOUT c:\test.txt -T -c -Sservername



Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -