我想從逗號分隔的表中將數據加載到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