使用ssis脚本任务将逗号分隔的平面文件加载到SQL Server表中

时间:2011-06-01 20:34:31

标签: ssis

我想将逗号分隔表中的数据加载到sql server上的临时表中。我正在使用此代码,它工作得很好。但由于它是一个“,”分隔文件,如果文件中的任何字段包含',',则此代码不起作用。在替换函数中,“,”也被替换。任何帮助

进口系统 导入System.Data 进口System.Math 导入Microsoft.SqlServer.Dts.Runtime 进口System.IO 导入system.Data.OleDb 导入Microsoft.SqlServer.DTSRuntimeWrap

Public Class ScriptMain

' The execution engine calls this method when the task executes.
' To access the object model, use the Dts object. Connections, variables, events,
' and logging features are available as static members of the Dts class.
' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
' 
' To open Code and Text Editor Help, press F1.
' To open Object Browser, press Ctrl+Alt+J.

Public Sub Main()
    Dts.TaskResult = Dts.Results.Failure
    Dim strFilePath As String = Dts.Variables("FilePath").Value.ToString
    Dim strCurrentZipFile As String = Dts.Variables("CurrentZipFile").Value.ToString
    Dim strConn As String = IO.Path.GetFileNameWithoutExtension(Dts.Variables("FilePath").Value.ToString)
    Dim strFields() As String = Dts.Variables("FilePath").Value.ToString.Split(",".ToCharArray())

    'Dts.Connections.Item(strConn).ConnectionString = strFilePath
    Dts.Connections.Item("EmpInfo").ConnectionString = strFilePath
    Dts.Variables("CurrentRawFile").Value = IO.Path.GetFileName(strCurrentZipFile)
    ' MsgBox(Dts.Variables("CurrentRawFile").Value)
    Dts.TaskResult = Dts.Results.Success


    ' The execution engine calls this method when the task executes.
    ' To access the object model, use the Dts object. Connections, variables, events,
    ' and logging features are available as static members of the Dts class.
    ' Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
    ' 
    ' To open Code and Text Editor Help, press F1.
    ' To open Object Browser, press Ctrl+Alt+J.


    Dim cm As ConnectionManager

    Dim con As OleDbConnection
    Dim cmd As New OleDbCommand()
    ' myADONETConnection = DirectCast(TryCast(Dts.Connections("Polldata").AcquireConnection(Dts.Transaction), SqlConnection), SqlConnection)

    '  MsgBox(myADONETConnection.ConnectionString, "PollData")

    Dim line1 As String = ""
    'Reading file names one by one
    Dim SourceDirectory As String = Dts.Variables("FilePath").Value.ToString
    cm = Dts.Connections("Polldata")
    Dim cmParam As Wrapper.IDTSConnectionManagerDatabaseParameters90
    cmParam = CType(cm.InnerObject, Wrapper.IDTSConnectionManagerDatabaseParameters90)
    con = CType(cmParam.GetConnectionForSchema(), OleDb.OleDbConnection)


    cmd.Connection = con
    'MsgBox(Dts.Variables("FilePath").Value.ToString)
    ' TODO: Add your code here
    '  Dim fileEntries As IO.DirectoryInfo = New IO.DirectoryInfo(SourceDirectory)
    ' MsgBox(fileEntries)
    ' For Each fileName As String In fileEntries.GetFiles()
    ' do something with fileName
    ' MsgBox(fileName)
    Dim columname As String = ""


    'Reading first line of each file and assign to variable
    Dim file2 As New System.IO.StreamReader(Dts.Variables("FilePath").Value.ToString) '(fileName)

    'Dim filenameonly As String = (((fileName.Replace(SourceDirectory, "")).Replace(".txt", "")).Replace("\", ""))
    'Create a temporary table 
    line1 = (" IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].tmp_empinfo" & "') AND type in (N'U'))DROP TABLE [dbo].tmp_empinfo" & " Create Table dbo.tmp_empinfo" & "(" & file2.ReadLine().Replace(",", " VARCHAR(100),") & " VARCHAR(100))").Replace(".txt", "")

    file2.Close()

    ' MsgBox(line1.ToString())
    cmd.CommandText = line1
    cmd.ExecuteNonQuery()


    'MsgBox("TABLE IS CREATED")

    'Writing Data of File Into Table
    Dim counter As Integer = 0
    Dim line As String = ""

    Dim SourceFile As New System.IO.StreamReader(Dts.Variables("FilePath").Value.ToString) '(fileName)
    While (InlineAssignHelper(line, SourceFile.ReadLine())) IsNot Nothing

        If counter = 0 Then
            columname = line.ToString()
            ' MsgBox("INside IF")
        Else

            ' MsgBox("Inside ELSE")
            Dim query As String = "Insert into dbo.tmp_empinfo" & "(" & columname & "  VALUES('" & line.Replace(",", "','").Replace("""", "") & "')"

            'Dim query As String = "Insert into dbo.tmp_empinfo" & "(" & columname & "  VALUES(" & strFields.ToString & ")"

            ' Dim query As String = "BULK INSERT dbo.tmp_empinfo FROM '" & strFilePath & "' WITH " & " ( " & " FIELDTERMINATOR = '|', " & " ROWTERMINATOR = '\n' " & " )"


            MsgBox(query.ToString())

            cmd.CommandText = query
            cmd.ExecuteNonQuery()
        End If

1 个答案:

答案 0 :(得分:0)

“我想将逗号分隔表中的数据加载到sql server上的临时表中”。你是说你基本上已经在数据库的表中有一个列,其中包含逗号分隔列表中的数据?例如,

SELECT column_name FROM schema.table

输出some_data,more_data,even_more_data,even,more_data等内容?你的问题是文本没有引用,所以当你尝试在目的地加载时,有些行最终会有额外的幻像列?

如果这是问题,那么我建议在源数据加载到源表之前引入引用的标识符。意思是,需要修复将数据导入该表的任何过程,以便您不必处理此类问题。如果无法完成,那么您必须在脚本组件或sql select语句中构建逻辑以将其拆分。此时解决问题的唯一方法是修复数据。

我是否误解了您的意图,或者这是否回答了您的问题?