读取包含SQL代码的文本文件,以在数据库中创建表

时间:2014-05-08 02:27:57

标签: vb.net visual-studio-2012 sql-server-2012

我需要代码来读取我的项目.txt目录中的bin\debug文件,该文件包含SQL代码,用于创建大小为936kb的表格

以下代码我只使用...

通过使用它,它给出了像表创建的结果,但它没有读取文件......数据库中没有任何内容

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

2 个答案:

答案 0 :(得分:0)

在readTextFile函数中,如果存在文件DOESN&T,它将仅尝试从文本文件中读取文本。如果文本文件存在,则该函数返回一个空字符串,如果文本文件不存在,该函数将抛出一个未找到文件的异常。

替换:

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

使用:

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

您可能还想要包含一个Else子句,以防文件不存在,因为它没有因为您正确处理错误而抛出错误。

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

答案 1 :(得分:0)

我的自我我找到了解决方案..我附上以下代码......现在正在创建所有表格..  确保文本文件中的每个Sql命令以结尾...因为我使用&#34; GO&#34; 关键字拆分文字......

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