sql server - How to add Text at the end of each line vb.net


I'm developing a program with VB.NET (2013), which works with a local database (sql server 2008 R2), The program is converting database tables into text files , so how to add some text after the last field of each row at the end of each line in that text file, Thanks, And sorry for my english

If mytable.HasRows Then
                Dim outputStream As StreamWriter = New StreamWriter(FileName & "table.txt", True, GetEncoding)
                Dim row As Integer = 0
                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() & ",")
                        End If
                        counter += 1
                    End While
                    row += 1

            End If

2 Answers: 

Using the example you have provided, you could replace

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


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

Here's a refactor of the code with your question answered:

Imports System.Data.SqlClient
Imports System.Data
Public Class Class1
    Public Sub Writefile()
        Dim ConnectionString As String = ""
        Using cnn As New SqlConnection(ConnectionString)
            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
                        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
                                            If c < ds.Tables(0).Columns.Count - 1 Then
                                            End If
                                    End Select
                        End Using
                    End Using
                End Using
            End Using
        End Using
    End Sub
End Class

This sample includes the database call as well. The "Using" blocks will ensure that your objects are disposed and closed appropriately, as well. I use this pattern and patterns like it all the time for writing text files.


More Articles

r - split string at every character

I want to split every character in a string and output it as comma or tab separated characters:I need to use file_in and file_out since I have very many lines.inputTTTGGCTTTGTGCAATGG........outputT,T,T,G,G,CT,T,T,GT,G,C,A,A,T,G,GI have used this, but it prints every character vertically: /usr/bin/p

libgit2sharp: how to set multiple values with same key

how do I set multiple values for a config-key? Some sections supports multiple values:[remote "origin"] url = git@github.com:schacon/simplegit-progit.git fetch = +refs/heads/master:refs/remotes/origin/master fetch = +refs/heads/qa/*:refs/remotes/origin/qa/*Something like this is not working in

Know of any setup tutorials for SQL Server Express and C#?

I'm a C# and MySQL developer, but I'm looking into Microsoft's SQL Server for a new project. I'm familiar with MySQL syntax and the .NET connector. Does anyone know of any decent tutorials that just cover the code to interact with SQL Server? No C# basics or SQL basics, just how to get the two tec

parsing - Parse JQ output through external bash function?

I want to parse out data out of a log file which consist of JSON sting and I wonder if there's a way for me to use a bash function to perform any custom parsing instead of overloading jq command.Command:tail errors.log --follow | jq --raw-output '. | [.server_name, .server_port, .request_file] | @ts

php - Laravel openssl_private_encrypt(): key param is not a valid private key

I am trying to connect to Chef API with Laravel using PHP-Chef. I have tried to set up my chef config with the data that I got from knife.rb in .chef folder. I have setup client and a key according to the instructions from Knife.rb. But I get:openssl_private_encrypt(): key param is not a valid priva

c++11 - C++ problem with understanding of counting char variables

I wrote a program that supposed to print the number of characters that i entered till it hits the '#' character. what i don't understand is, when i input in the console more than one character (say "hello world") the program count all the characters in one iteration. why does it count all the charac

powershell - Table - Count Column

I have a PowerShell script that coverts JSON from an API and creates a table output.How would I TOTAL (add or count) the column 'TotalEndpoints' and display the total number of Enpoints?Here is the creation of the table(Invoke-RestMethod @Params).Sites | Format-Table SiteName,SiteId,TotalEndpoints

c++ - GCC Cross compile to a i586 architecture (Vortex86DX)

I have Ubuntu 12.01 with gcc 4.8.2 and would like to cross compile for the Vortex86DX CPU running an old 2.6.23 kernel.I´m trying the following testing code:#include <iostream>int main(){ std::cout << "Hello world" << std::endl;}That is compiled using the following command line:g

Using gstreamer plugins bad in android

I am trying to show h264 encodes streams inside an android application using the GStreamer SDK and Android NDK. My Android.mk looks like:LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := AppLOCAL_SRC_FILES := App.cppLOCAL_SHARED_LIBRARIES := gstreamer_androidLOCAL_LDLIBS := -llog -

c++ - How To Return Elapsed Time From steady_clock as a Primitive Data Type (double)

First and foremost, let me say that I just starting using this library yesterday, so my understanding of it is still fairly basic. I'm trying to capture the FPS of a vision processing program I'm creating and output it to a screen using the chrono library. In my case, I need to cast the elapsed time