将数据从文本文件导入MS SQL Server的最佳方法

时间:2015-03-20 12:34:46

标签: sql-server vb.net performance sqlbulkcopy

需要你的意见。目前正在VB.NET中开发一个应用程序。

我有一个包含超过一千行的文本文件。每行包含需要插入数据库的数据。行的样本如下:

0001--------SCOMNET--------0.100---0.105

乍一看,有人可能会发现每个列都是用制表符分隔的,但实际上每个列都是用空格分隔的(我用' - '表示空格,因为某种程度上无法通过SO文本编辑器显示空白空格)。

第一列由

定义
Substring(0, 12) which is the data [0001--------]

第二栏

Substring(12, 12) in which the data is [SCOMNET-----] 

第三栏是

Substring(24, 8) in which the data is [---0.100] 

,最后一栏是

Substring(32, 8) in which the data is [---0.105]

我的初始是提取文本文件的行并作为字符串列表存储,然后在循环时,使用SubString()函数分隔列表中的每个字符串项并将其插入一个一直到列表结束。但这无疑需要时间。

在我的场景中,我如何利用SqlBulkCopy?或者,如果有任何其他方法来实现更快的插入?说;

  • 打开文件
  • 启动循环
    • 读取行
    • 使用子字符串
    • 分隔行中的每一列
    • 保存在DataTable中
  • 结束循环
  • BCP(数据表)

2 个答案:

答案 0 :(得分:1)

这包括一种可以更有效地阅读文本文件的方法。

    Sub readFixWidthTextFileIntoSqlTable()

    Dim sqlConn As New SqlConnection("Connection String Goes Here")
    sqlConn.Open()
    Dim sqlComm As New SqlCommand
    sqlComm.Connection = sqlConn
    sqlComm.CommandType = CommandType.Text
    sqlComm.CommandText = "INSERT INTO YourTableNameHere VALUES(@Field1, @Field2, @Field3, @Field4)"

    sqlComm.Parameters.Add("@Field1", SqlDbType.Text)
    sqlComm.Parameters.Add("@Field2", SqlDbType.Text)
    sqlComm.Parameters.Add("@Field3", SqlDbType.Text)
    sqlComm.Parameters.Add("@Field4", SqlDbType.Text)



    Using IFReader As New FileIO.TextFieldParser(FileNameWithPath)
        IFReader.TextFieldType = FileIO.FieldType.FixedWidth
        IFReader.SetFieldWidths(12, 12, 8, 8)

        While Not IFReader.EndOfData
            Dim fields As String() = IFReader.ReadFields

            sqlComm.Parameters("@Field1").Value = fields(0)
            sqlComm.Parameters("@Field2").Value = fields(1)
            sqlComm.Parameters("@Field3").Value = fields(2)
            sqlComm.Parameters("@Field4").Value = fields(3)
            sqlComm.ExecuteNonQuery()
        End While

    End Using

    sqlConn.Close()
End Sub

答案 1 :(得分:0)

你已经非常正确了。这种方法是我经常采用的方法。这里有一些示例代码可以帮助您入门。这只是一个例子,绝对没有验证或没有考虑表上的主键。如果您想查看目标表结构的更多详细信息,那么我可以更具体地说明这个例子。

 Read_File:

    Dim sFileContents As String = ""

    Using sRead As New StreamReader("e:\ExampleFile.txt")
        sFileContents = sRead.ReadToEnd
    End Using

    Dim sFileLines() As String = sFileContents.Split(vbCrLf)

Connect_To_DB:

    Dim sqlConn As New SqlConnection

    sqlConn.ConnectionString = "Data Source=YourServerName;Initial Catalog=YourDbName;Integrated Security=True"
    sqlConn.Open()

Setup_DataTable:

    Dim ExampleTable As New DataTable
    ExampleTable.Load(New SqlCommand("Select Top 0 * From Example_Table", sqlConn).ExecuteReader)
    'This is not absolutely necessary but avoids trouble with NOT NULL columns (like keys)'
    For Each dcColumn As DataColumn In ExampleTable.Columns : dcColumn.AllowDBNull = True : Next dcColumn

Save_To_DataTable:

    For Each sLine In sFileLines

        Dim ExampleRow As DataRow = ExampleTable.NewRow

        ExampleRow("First_Column_Name") = sLine.Substring(0, 12).TrimEnd
        ExampleRow("Second_Column_Name") = sLine.Substring(12, 12).TrimEnd
        ExampleRow("Third_Column_Name") = sLine.Substring(24, 8).TrimEnd
        ExampleRow("Fourth_Column_Name") = sLine.Substring(32, 8).TrimEnd

        ExampleTable.Rows.Add(ExampleRow)

    Next

Update_Database:

    If ExampleTable.Rows.Count <> 0 Then

        Dim sqlBulk As SqlBulkCopy = New SqlBulkCopy(sqlMvConnection)

        sqlBulk.DestinationTableName = "Example_Table"
        sqlBulk.WriteToServer(ExampleTable)

    End If

Disconnect_From_DB:

    sqlConn.Close()

此外,如上所述,如果您有权访问它,SSIS将在一瞬间完成此任务。