我最终尝试使用SqlBulkCopy
将文件中的行插入SQL Server数据库。我的代码如下。
当我尝试向数据表添加太多行时出现问题。我不确定限制是什么,但我的文件有500万行,我在行上得到OutOfMemoryException
:
newRow.ItemArray = objFields.ToArray()
所以我认为这与数据表的限制有关。
有没有办法可以逐步添加行,只是一次插入一些,或者我在这里做错了什么我可以改变?
使用较小的文件,该过程可以正常工作。
由于
Public Sub bulk_insert(file_name, delimiter, table_name)
' create a data table from the file
' ---------------------------------
Dim lines = IO.File.ReadAllLines(file_name)
Dim tbl = New DataTable
Dim colCount = lines.First.Split(delimiter).Length
' add the correct number of columns to the table
' ----------------------------------------------
For i As Int32 = 1 To colCount
tbl.Columns.Add(New DataColumn("Column_" & i, GetType(String)))
Next
' add the lines to the data table
' -------------------------------
For Each line In lines
Dim objFields = From field In line.Split(delimiter) Select CType(field, Object)
Dim newRow = tbl.Rows.Add()
newRow.ItemArray = objFields.ToArray()
Next
' create a connection to the database
' -----------------------------------
Dim myConn = New SqlConnection(Main.this_page.sql_connection_string)
' start the Bulk Copy
Dim SqlBulkCopy = New SqlBulkCopy(myConn)
SqlBulkCopy.BatchSize = 10000
SqlBulkCopy.BulkCopyTimeout = 10000
' open the connection
' -------------------
myConn.Open()
' perform the copy
' ----------------
SqlBulkCopy.DestinationTableName = table_name
SqlBulkCopy.WriteToServer(tbl)
' cloe the connection
' -------------------
myConn.Close()
End Sub
编辑:测试后我发现错误发生时,行数是2097152
修改:由于我不想回答我自己的问题,我不能将其放在评论中,因此我根据下面的the_lotus提供的答案将我的结果包括在内。下面的代码解决了这个问题:
Public Sub bulk_insert(file_name, delimiter, table_name)
' create a connection to the database
' -----------------------------------
Dim myConn = New SqlConnection(Main.this_page.sql_connection_string)
' open the connection
' -------------------
myConn.Open()
' create a data table from the file
' ---------------------------------
Dim lines = IO.File.ReadAllLines(file_name)
Dim tbl = New DataTable
Dim colCount = lines.First.Split(delimiter).Length
' add the correct number of columns to the table
' ----------------------------------------------
For i As Int32 = 1 To colCount
tbl.Columns.Add(New DataColumn("Column_" & i, GetType(String)))
Next
Dim x As Integer = 0
While x < lines.Count - 1
' clear the data table
' --------------------
tbl.Clear()
' add the lines to the data table in 1M row increments
' ---------------------------------------------------
If x + 1000000 > lines.Count - 1 Then
For y = x To lines.Count - 1
Dim objFields = From field In lines(y).Split(delimiter) Select CType(field, Object)
Dim newRow = tbl.Rows.Add()
newRow.ItemArray = objFields.ToArray()
Next
x = lines.Count - 1
Else
For y = x To x + 1000000
Dim objFields = From field In lines(y).Split(delimiter) Select CType(field, Object)
Dim newRow = tbl.Rows.Add()
newRow.ItemArray = objFields.ToArray()
Next
x += 1000000
End If
' start the Bulk Copy
Dim SqlBulkCopy = New SqlBulkCopy(myConn)
SqlBulkCopy.BatchSize = 1000000
SqlBulkCopy.BulkCopyTimeout = 1000
' perform the copy
' ----------------
SqlBulkCopy.DestinationTableName = table_name
SqlBulkCopy.WriteToServer(tbl)
End While
' cloe the connection
' -------------------
myConn.Close()
End Sub
答案 0 :(得分:1)
你的RAM已经用完......你的数据在行和tbl中重复。您已指定BatchSize为10000,您无需加载更多。
我会:
如果您有500万行,每行有100个字符的数据。这就是你的行变量中的500mb数据。然后在tbl中复制此数据,DataTable必须为每个数据点和行提供额外信息。