Solved

SQL Data Reader and Stream writer only returning 1 row

Posted on 2013-05-17
2
596 Views
Last Modified: 2013-05-17
I have the following code that is pulling data from my sql table and putting it in a cvs file.

I can only get it working for the first row. That table has 145 rows in it.

    Protected Sub cmdDownload_Click() Handles cmdDownload.Click

        Dim constr As String = ConfigurationManager.ConnectionStrings("baminterchangerConnectionString").ConnectionString
        Using sqlConnectionObject As New SqlConnection(constr)

            Dim sqlQueryCommand As New SqlCommand("SELECT * FROM [dbo].[imports]", sqlConnectionObject)
            Dim sqlResultSet As SqlDataReader

            sqlConnectionObject.Open()
            sqlResultSet = sqlQueryCommand.ExecuteReader()

            Dim savedFiles As String
            Dim filename As String = (GetCurrent.Name)
            savedFiles = Path.GetFileName(filename & ".csv")

            Dim Excel As String = Server.MapPath("temp\" & savedFiles)

            Using streamWriterObject As New StreamWriter(Excel)
                    Do While sqlResultSet.Read
                        sqlResultSet.GetSqlValue(0)
                        sqlResultSet.GetSqlValue(1)
                        'Write a line of stream of each value of the columns in the table to the stream object
                        streamWriterObject.WriteLine(sqlResultSet(0).ToString() & "," & sqlResultSet(1).ToString())
                        sqlResultSet.NextResult()
                    Loop
                    MessageBox("Successfully Uploaded CSV File")
            End Using

        End Using


    End Sub

End Class

Open in new window

0
Comment
Question by:bignadad
2 Comments
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 total points
ID: 39176507
Since you only have one resultset the following line needs to be commented out:
sqlResultSet.NextResult()

The Read method skips to the next row.
0
 
LVL 2

Author Closing Comment

by:bignadad
ID: 39176510
Worked perfect!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now