2014-04-17 132 views
0

我正在開發一個VB.NET程序(2013),該程序適用於本地數據庫(sql server 2008 R2), 該程序正在轉換數據庫表爲文本文件, 所以如何每行的在該文本文件, 感謝每行末尾的最後一個域之後添加一些文字,很遺憾,我的英語如何在每行的末尾添加文本vb.net

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 
+1

你沒有真正問過很多問題。 MyConnection在您的文章中看起來相當不必要。 WriteLine將添加新的行字符,所以如果您想添加某些內容,請在調用之前進行添加。 – LarsTech

+0

你想替換colums 0,3和7的內容嗎?你想在每一行的末尾寫什麼?請更好地解釋你的要求。示例文本行可能非常有用 – Steve

+0

實際上,我不想更改列的內容,我只是想在文本文件的列輸出之間添加文本,而對於每行的結尾,我希望添加類似「True」的例子 – dKorey

回答

0

使用您所提供的例子,你可以替換

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

With:

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

謝謝,它真的有竅門 – dKorey

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 

此示例包括數據庫調用也是如此。 「使用」塊將確保您的物體被妥善處理和關閉。我一直使用這種模式和模式來編寫文本文件。

+0

謝謝你的幫助,它也適用於我。 – dKorey

相關問題