如何在每行vb.net的末尾添加Text

时间:2014-04-17 15:21:30

标签: sql-server vb.net text add

我正在使用VB.NET(2013)开发一个程序,该程序与本地数据库(sql server 2008 R2)一起使用, 该程序正在将数据库表转换为文本文件, 那么如何在该文本文件中每行末尾的每一行的最后一个字段后添加一些文本, 谢谢,对不起我的英文

If mytable.HasRows Then
                Dim outputStream As StreamWriter = New StreamWriter(FileName & "table.txt", True, GetEncoding)
                Dim row As Integer = 0
                myConnection.open()
                Do While mytable.Read                    
                    Dim header As Integer = 0
                    Dim counter As Integer = 0
                    Dim fieldCount As Integer = mytable.FieldCount - 1

                    While counter <= fieldCount
                        If counter = 0 Then
                            outputStream.Write("None ," )
                        End If
                        If counter = 3 Then
                            outputStream.Write("123456 ," )
                        End If
                        If counter = 7 Then
                            outputStream.Write("None ," )
                        End If

                        If counter <> fieldCount Then
                            outputStream.Write(mytable(counter).ToString() & ",")
                        Else
                            outputStream.WriteLine(mytable(counter).ToString())
                        End If
                        counter += 1
                    End While
                    row += 1

                Loop
                mytable.Close()
                outputStream.Close()
                myConnection.Close()
            End If

2 个答案:

答案 0 :(得分:0)

使用您提供的示例,您可以替换

                    If counter <> fieldCount Then
                        outputStream.Write(mytable(counter).ToString() & ",")
                    Else
                        outputStream.WriteLine(mytable(counter).ToString())
                    End If

使用:

                    If counter <> fieldCount Then
                        outputStream.Write(mytable(counter).ToString() & ",")
                    Else
                        outputStream.WriteLine(mytable(counter).ToString() & "What ever you want to write")
                    End If

答案 1 :(得分:0)

这是代码的重构,您的问题已经回答:

Imports System.Data.SqlClient
Imports System.Data
Public Class Class1
    Public Sub Writefile()
        Dim ConnectionString As String = ""
        Using cnn As New SqlConnection(ConnectionString)
            cnn.Open()
            Using cmd As SqlCommand = cnn.CreateCommand
                cmd.CommandType = System.Data.CommandType.Text
                cmd.CommandText = "SELECT * FROM SomeTable"
                Using da As New SqlDataAdapter
                    Using ds As New DataSet
                        da.Fill(ds)
                        Using sw As New System.IO.StreamWriter("C:\myfile.txt")
                            For Each r As DataRow In ds.Tables(0).Rows
                                Dim output As New System.Text.StringBuilder
                                For c As Integer = 0 To ds.Tables(0).Columns.Count - 1
                                    Select Case c
                                        Case 0
                                            output.Append("none ,")
                                        Case 3
                                            output.Append("123456 ,")
                                        Case 7
                                            output.Append("none ,")
                                        Case Else
                                            output.Append(r(c).ToString)
                                            If c < ds.Tables(0).Columns.Count - 1 Then
                                                output.Append(",")
                                            End If
                                    End Select
                                    sw.WriteLine(output.ToString)
                                Next
                            Next
                        End Using
                    End Using
                End Using
            End Using
        End Using
    End Sub
End Class

此示例还包括数据库调用。 &#34;使用&#34;块将确保您的对象也被适当地处理和关闭。我一直使用这种模式和模式来编写文本文件。