需要使用vba将信息从文本文件导入到sql

时间:2015-10-01 20:17:34

标签: sql vba file text import

这就是我的文本文件的样子 - 左边是变量,右边是数据

; ------- Test #01 -------


Method      , Ping
;--- Common properties ---
;DestFolder , Root\Black\
RMAgent     , Black
Title       , Black - Agent not connected
Comment     , Ping mra itself
RelatedURL  , 
NamePattern , %agent% - Agent not connected
CmntPattern , Ping %host%
ScheduleMode, Regular
Schedule    , 
Interval    , 600
Alerts      , Send AM Email
ReverseAlert, No
UnknownIsBad, Yes
WarningIsBad, Yes
UseCommonLog, Yes
PrivLogMode , Default
CommLogMode , Default

这是我目前的代码

Public Sub Load_Module1(ByVal FileName As String, ByVal LogFile As String)

End Sub

Sub main()

    Dim fs As FileStream
    Dim sw As StreamReader
    Dim strNewFileName As String
    Dim strFileExt As String
    Dim intPos As Integer
    Dim strColumnData As String() = Nothing
    Dim intRow As Integer = 1
    Dim strLine As String
    'Dim strSubString As String

    '-----------------------------------------------------------------------
    ' Do the Common Table
    '-----------------------------------------------------------------------
    Dim strDBConnection As String = "Data Source=*******;Initial Catalog=*******;User Id=*******;Password=*******"
    Dim cn As New SqlConnection(strDBConnection)
    Dim cmd As New SqlCommand("appsp_host_import", cn)
    Const filename As String = "C:\Temp\HostMonitorTests2015-09-11change.txt.txt"
    Dim dataArray() As String
    Dim filenumber As Integer = FreeFile()
    Dim strmethod As String
    Dim strDestFolder As String
    Dim strRMAgent As String
    Dim strTitle As String
    Dim strComment As String
    Dim strRelatedURL As String
    Dim strNamePattern As String
    Dim strCmntPattern As String
    Dim strScheduleMode As String
    Dim strSchedule As String
    Dim strInterval As Integer
    Dim strAlerts As String
    Dim strReverseAlert As Boolean
    Dim strUnknownIsBad As Boolean
    Dim strWarningIsBad As Boolean
    Dim strUseCommonLog As Boolean
    Dim strPrivLogMode As String
    Dim strCommLogMode As String
    Dim strtype As String
    Dim strImportRow As String
    Dim strLastOrderNum As String = ""
    Dim intImportID As Integer
    Dim intLineNum As Integer
    Dim intQtyOrdered As Integer




    Try
        fs = New FileStream(filename, FileMode.Open, FileAccess.Read)
        sw = New StreamReader(filename)
    Catch e As Exception
        Exit Sub
    End Try
    strLine = sw.ReadLine()
    'Dim TextLine As String
    '' Open file.

    '' Loop until end of file. 
    'Do While Not EOF(1)
    '    ' Read line into variable.
    '    TextLine = LineInput(1)
    '    ' Display result in a message box.
    '    MsgBox("End of file reached at " & TextLine)
    'Loop
    'FileClose(1)
    Dim pos As Integer = InStr("; ------- Test #01 -------")

    While Not sw.EndOfStream
        If filename.Contains("; ------- Test #01 -------") Then
            cn.Open()
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.Add("@DestFolder", SqlDbType.VarChar, 512)
            cmd.Parameters.Add("@RMAgent", SqlDbType.VarChar, 512)
            cmd.Parameters.Add("@Title", SqlDbType.VarChar, 512)
            cmd.Parameters.Add("@Comment", SqlDbType.VarChar, 512)
            cmd.Parameters.Add("@RelatedURL", SqlDbType.VarChar, 512)
            cmd.Parameters.Add("@NamePattern", SqlDbType.VarChar, 512)
            cmd.Parameters.Add("@CmntPattern", SqlDbType.VarChar, 512)
            cmd.Parameters.Add("@ScheduleMode", SqlDbType.VarChar, 512)
            cmd.Parameters.Add("@Schedule", SqlDbType.VarChar, 512)
            cmd.Parameters.Add("@Interval", SqlDbType.Int)
            cmd.Parameters.Add("@Alerts", SqlDbType.VarChar, 512)
            cmd.Parameters.Add("@ReverseAlert", SqlDbType.Bit)
            cmd.Parameters.Add("@UnknownIsBad", SqlDbType.Bit)
            cmd.Parameters.Add("@WarningIsBad", SqlDbType.Bit)
            cmd.Parameters.Add("@UseCommonLog", SqlDbType.Bit)
            cmd.Parameters.Add("@PrivLogMode", SqlDbType.VarChar, 512)
            cmd.Parameters.Add("@CommLogMode", SqlDbType.VarChar, 512)
            cmd.Parameters.Add("@SyncCounters", SqlDbType.Bit)
            cmd.Parameters.Add("@SyncAlerts", SqlDbType.Bit)
            cmd.Parameters.Add("@DependsOn", SqlDbType.VarChar, 512)
            cmd.Parameters.Add("@Testspecific", SqlDbType.VarChar, 512)
        End If
        strtype = Mid(strLine, 13, 1)
        Select Case strtype
            Case "Method"
                strmethod = Trim(Mid(strLine, 14, 99))
            Case ";DestFolder"
                strDestFolder = Trim(Mid(strLine, 14, 99))
            Case "RMAgent"
                strRMAgent = Trim(Mid(strLine, 14, 99))
            Case "Title"
                strTitle = Trim(Mid(strLine, 14, 99))
            Case "Comment"
                strComment = Trim(Mid(strLine, 14, 99))
            Case "RelatedURL"
                strRelatedURL = Trim(Mid(strLine, 14, 99))
            Case "NamePattern"
                strNamePattern = Trim(Mid(strLine, 14, 99))
            Case "CmntPattern"
                strCmntPattern = Trim(Mid(strLine, 14, 99))
            Case "ScheduleMode"
                strScheduleMode = Trim(Mid(strLine, 14, 99))
            Case "Schedule"
                strSchedule = Trim(Mid(strLine, 14, 99))
            Case "Interval"
                strInterval = Trim(Mid(strLine, 14, 99))
            Case "Alerts"
                strAlerts = Trim(Mid(strLine, 14, 99))
            Case "ReverseAlert"
                strReverseAlert = Trim(Mid(strLine, 14, 99))
            Case "UnknownIsBad"
                strUnknownIsBad = Trim(Mid(strLine, 14, 99))
            Case "WarningIsBad"
                strWarningIsBad = Trim(Mid(strLine, 14, 99))
            Case "UseCommonLog"
                strUseCommonLog = Trim(Mid(strLine, 14, 99))
            Case "PrivLogMode"
                strPrivLogMode = Trim(Mid(strLine, 14, 99))
            Case "CommLogMod"
                strCommLogMode = Trim(Mid(strLine, 14, 99))
                cmd.ExecuteNonQuery()
                cmd.Parameters("@Reset").Value = 0
        End Select
        filenumber = 0
        'sw.Close()
        'fs.Close()
        cn.Close()
    End While
    FileClose(1)

    'Try
    '    cn.Open()
    '    cmd.CommandType = CommandType.StoredProcedure
    '    cmd.Parameters.Add("@DestFolder", SqlDbType.VarChar, 512)
    '    cmd.Parameters.Add("@RMAgent", SqlDbType.VarChar, 512)
    '    cmd.Parameters.Add("@Title", SqlDbType.VarChar, 512)
    '    cmd.Parameters.Add("@Comment", SqlDbType.VarChar, 512)
    '    cmd.Parameters.Add("@RelatedURL", SqlDbType.VarChar, 512)
    '    cmd.Parameters.Add("@NamePattern", SqlDbType.VarChar, 512)
    '    cmd.Parameters.Add("@CmntPattern", SqlDbType.VarChar, 512)
    '    cmd.Parameters.Add("@ScheduleMode", SqlDbType.VarChar, 512)
    '    cmd.Parameters.Add("@Schedule", SqlDbType.VarChar, 512)
    '    cmd.Parameters.Add("@Interval", SqlDbType.Int)
    '    cmd.Parameters.Add("@Alerts", SqlDbType.VarChar, 512)
    '    cmd.Parameters.Add("@ReverseAlert", SqlDbType.Bit)
    '    cmd.Parameters.Add("@UnknownIsBad", SqlDbType.Bit)
    '    cmd.Parameters.Add("@WarningIsBad", SqlDbType.Bit)
    '    cmd.Parameters.Add("@UseCommonLog", SqlDbType.Bit)
    '    cmd.Parameters.Add("@PrivLogMode", SqlDbType.VarChar, 512)
    '    cmd.Parameters.Add("@CommLogMode", SqlDbType.VarChar, 512)
    '    cmd.Parameters.Add("@SyncCounters", SqlDbType.Bit)
    '    cmd.Parameters.Add("@SyncAlerts", SqlDbType.Bit)
    '    cmd.Parameters.Add("@DependsOn", SqlDbType.VarChar, 512)
    '    cmd.Parameters.Add("@Testspecific", SqlDbType.VarChar, 512)
    '    'cmd.Parameters("@Reset").Value = 1


    'Catch e As Exception
    '    Exit Sub
    'End Try

    If Not sw.EndOfStream Then
        strLine = sw.ReadLine()     'Get the header line
    End If

    While Not sw.EndOfStream
        Try
            strLine = sw.ReadLine()
            intRow = intRow + 1
            If UCase(Mid(strLine, 1, 3)) = "YES" Or UCase(Mid(strLine, 1, 3)) = "NO " Then
                cmd.Parameters("@DestFolder").Value = UCase(Trim(Mid(strLine, 1, 3)))
                cmd.Parameters("@RMAgent").Value = Trim(Mid(strLine, 7, 6))
                cmd.Parameters("@Title").Value = Trim(Mid(strLine, 14, 15))
                cmd.Parameters("@Comment").Value = Trim(Mid(strLine, 14, 15))
                cmd.Parameters("@RelatedURL").Value = Trim(Mid(strLine, 47, 5))
                cmd.Parameters("@NamePattern").Value = Trim(Mid(strLine, 52, 5))
                cmd.Parameters("@CmntPattern").Value = Trim(Mid(strLine, 60, 8))
                cmd.Parameters("@ScheduleMode").Value = Trim(Mid(strLine, 69, 8))
                cmd.Parameters("@Schedule").Value = Trim(Mid(strLine, 83, 6))
                cmd.Parameters("@Interval").Value = CInt(Trim(Mid(strLine, 78, 4)))
                cmd.Parameters("@Alerts").Value = Trim(Mid(strLine, 83, 6))
                cmd.Parameters("@ReverseAlert").Value = 0
                cmd.Parameters("@UnknownIsBad").Value = 0
                cmd.Parameters("@WarningIsBad").Value = 0
                cmd.Parameters("@UseCommonLog").Value = 0
                cmd.Parameters("@PrivLogMode").Value = Trim(Mid(strLine, 175, 7))
                cmd.Parameters("@CommLogMode").Value = Trim(Mid(strLine, 175, 7))
                cmd.Parameters("@SyncCounters").Value = 0
                cmd.Parameters("@SyncAlerts").Value = 0
                cmd.Parameters("@DependsOn").Value = Trim(Mid(strLine, 175, 7))
                cmd.Parameters("@Testspecific").Value = Trim(Mid(strLine, 175, 7))
                cmd.ExecuteNonQuery()
                cmd.Parameters("@Reset").Value = 0
            End If
        Catch e As Exception
            Exit Sub
        End Try
    End While
    sw.Close()
    fs.Close()
    cn.Close()

    'Move the file
    intPos = filename.Length
    While intPos > 0
        If Mid(filename, intPos, 1) = "  " Then
            Exit While
        End If
        intPos = intPos - 1
    End While
    strNewFileName = Left(filename, intPos)
    strNewFileName = strNewFileName & "Archive\" & Mid(filename, intPos + 1, 999)
    strFileExt = "_" & Now.ToString("yyyyMMdd_hhmmss") & ".txt"
    strNewFileName = strNewFileName.Replace(".txt", strFileExt)
    Try
        System.IO.File.Move(filename, strNewFileName)
    Catch e As Exception
    End Try

End Sub

2 个答案:

答案 0 :(得分:0)

这是第一个问题:

strtype = Mid(strLine, 13, 1)

应该是

strtype = Mid(strLine, 1, 13)

或更具可读性

strtype = Left(strLine, 13)

VBA什么时候有Try ... Catch?这段代码是否编译?

答案 1 :(得分:0)

    Try
        cn.Open()
        cmd = new SqlCommand(srtQuery, cn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.Add("@Method", SqlDbType.VarChar)

        .
        .
        cmd.Parameters.Add("@Testspecific", SqlDbType.VarChar, 512)
        cmd.ExecuteNonQuery()

   Catch sqlEx as SqlException
   'Do something about the exception

   Catch ex as Exception
   Throw 'Re-throw any other exception


   Finally
  cn.Close();
   End Try