2011-06-01 36 views
0

我想從逗號分隔的表中將數據加載到sql server上的臨時表中。我使用這個代碼,它工作得很好。但由於它是一個「,」分隔文件,如果文件中的任何字段包含「,」,則此代碼不起作用。如在替換功能中,「,」也被取代。任何幫助使用ssis腳本任務將逗號分隔的平面文件加載到SQL服務器表中

進口系統 進口System.Data 進口System.Math 進口Microsoft.SqlServer.Dts.Runtime 進口System.IO 進口system.Data.OleDb 進口Microsoft.SqlServer.DTSRuntimeWrap

公共類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 

回答

0

「我想從逗號分隔的表中加載數據到SQL服務器上的臨時表」。你是說你基本上已經在包含逗號分隔列表中的數據的數據庫中的表中有一列嗎?例如,

SELECT列名 FROM schema.table

輸出類似some_data,more_data,even_more_data,甚至,more_data?而你的問題是文本沒有被引用,所以當你試圖在你的目的地中加載它時,一些行最終會有額外的幻像列?

如果這是問題,那麼我建議在源數據加載到源表之前在源數據中引入帶引號的標識符。這意味着,無論將數據導入該表的過程都需要修復,以便不必處理這類問題。如果無法完成,那麼您必須在您的腳本組件或sql select語句中構建邏輯以適當地分割它。解決此問題的唯一方法是修復數據。

我誤解了你的意圖嗎?或者這是否回答你的問題?

相關問題