vb.net reader.read()无限循环

时间:2017-01-12 15:51:03

标签: .net vb.net vb.net-2010

使用while reader.read()进入无限循环

试图摆脱无限循环

这是完整的代码

Dim ConnectionString As String
    ConnectionString = "integrated security=SSPI;data source=SMG-BPCI-DB01;" &
    "persist security info=False;initial catalog=MOSAIC"

    Dim con As New SqlConnection(ConnectionString)
    Try
        con.Open()
    Catch ex As SqlException
        Console.WriteLine("Error: " & ex.ToString())
    End Try
    Dim sql As String = " Select Name, [Description], Type from Tbl; "

Dim cmd As New SqlCommand(sql, con)
    Dim sw As StreamWriter = New StreamWriter("C:\Export.csv")
    Dim reader As SqlDataReader = cmd.ExecuteReader()

Dim sep As String = "|"
    Dim builder As New System.Text.StringBuilder

While reader.Read()
        'Get data row by row
        For i = 0 To reader.FieldCount - 1
            builder.Append(sep).Append(reader(i).ToString)
        Next
        sw.WriteLine(builder.ToString())
    End While
    sw.Close()
    reader.Close()

任何想法!!

1 个答案:

答案 0 :(得分:0)

下面的代码处理了这个问题。

builder,clear()