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.
| 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 DecimalDim column_2 As DateTimeDim column_3 As StringDim column_4 As Decimal'Create a CSV fileDim oWriter As System.IO.StreamWriteroWriter = System.IO.File.CreateText("C:\...\Temp.csv")'Connection String and SQLConnectDim connstring As String = "server=server;database=database;user id=user;password=password"Dim conn As New SqlConnection(connstring)'Create Selection QueryDim selectSQL As String = "SELECT ACCT, DATE, DOW, KWH FROM SENSUS3 WHERE (ACCT = " & txtAcct.Text & ")"'Create Selection CommandDim cmd As New SqlCommand(selectSQL, conn)'Set cmd Propertycmd.CommandType = CommandType.Text'Open Databaseconn.Open()'Create a new Reader and execute the ReaderDim 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 WhileoWriter.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) |
 |
|
|
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. |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-06-02 : 14:28:31
|
Np. Hope you figured the reason too |
 |
|
|
CorbanDavies
Starting Member
3 Posts |
Posted - 2010-06-02 : 15:03:30
|
| Yep... It seems so obvious when someone else points it out. |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
|
|
|
|
|