Question

I need code to read a .txt file which is in my project bin\debug directory that contains SQL code to create tables in a large number it size of 936kb

This following code only I'm using...

By using this it gives result like table created but it is not reading the file... there is nothing in the database

Public Function readTextFile(ByVal fileName As String) As String
        Dim strContent As String()
        Dim x As String = ""
        Try
            'fileName = "CSYSS802.txt"

            If Not System.IO.File.Exists(fileName) Then
                'o Until EOF()
                strContent = System.IO.File.ReadAllLines(fileName)


                For Each Str As String In strContent
                    x = x + Str
                Next

                readTextFile = x
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        readTextFile = x
    End Function

    Public Sub createTable(ByVal vdbs As String, ByVal file As String)
        username = frmlogin.txtusername.Text
        password = frmlogin.txtusername.Text
        vsvr = vServer
        vdb = Trim$(vdbs)
        strCon1 = "Server=" & vsvr & ";Database=" & vdb & ";uid=" & username & ";pwd=" & password & ";"
        sqlCon1 = New SqlClient.SqlConnection(strCon1)
        sqlCon1.Open()
        Dim arr() As String
        arr = Split(readTextFile(file), "GO")
        Dim i As String

        For Each i In arr
            If i <> "" Then
                Dim cmd2 As New SqlClient.SqlCommand("" & i & "")
                cmd2.CommandType = CommandType.Text
                cmd2.ExecuteNonQuery()
            End If
        Next
    End Sub
Was it helpful?

Solution 2

My Self I had Found The solution..I attache the Following Code...It now Creating All tables Properly.. Make sure that each Sql Commands in your Text File ends with go.. because i used "GO" Keyword to split the text...

Public Sub createTable(ByVal vdbs As String, ByVal file As String)
    username = frmlogin.txtusername.Text
    password = frmlogin.txtusername.Text
    vsvr = vServer
    vdb = Trim$(vdbs)
    strCon1 = "Server=" & vsvr & ";Database=" & vdb & ";uid=" & username & ";pwd=" & password & ";"
   sqlCon1 = New SqlClient.SqlConnection(strCon1)
    sqlCon1.Open()
    Dim arr() As String
    arr = Split(readTextFile(file), " GO ")
    Dim i As String

For Each i In arr
        If i <> "" Then
            Dim cmd2 As New SqlClient.SqlCommand("" & i & "", sqlCon1)
            cmd2.CommandType = CommandType.Text
            cmd2.ExecuteNonQuery()
        End If
    Next
End Sub
 Public Function readTextFile(ByVal file As String) As String
    Dim fso As New System.Object
    Dim ts As Scripting.TextStream
    Dim sLine As String

    fso = CreateObject("Scripting.FileSystemObject")
    ts = fso.openTextFile(file)

    Do Until ts.AtEndOfStream
        sLine = sLine & " " & ts.ReadLine

    Loop
    ts.Close()
    fso = Nothing
    readTextFile = sLine

End Function

OTHER TIPS

In the readTextFile function, it will only attempt to read the text from the text file if the file DOESN'T exist. If the text file exists then the function returns an empty string and if the text file doesn't exist, the function will throw a file not found exception.

Replace:

If Not System.IO.File.Exists(fileName) Then

with:

If System.IO.File.Exists(fileName) = True Then

You might also want to include an Else clause in case the file doesn't exist as it won't throw an error since you have handled it correctly.

If System.IO.File.Exists(fileName) = True Then

     strContent = System.IO.File.ReadAllLines(fileName)

     For Each Str As String In strContent
         x &= Str
     Next

     Return x

Else

     MessageBox.Show("The file '" & fileName & "' does not exist.")
     Return ""

End If
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top