如何将大量数据从asp.net gridview批量插入到PostgreSQL数据库中,我尝试使用insert语句,但需要很长时间才能完成。
我找到了一个在sql服务器上执行相同任务的代码示例, 但是使用postgresql时我不知道该怎么做。
这是sql server的代码,效果很好
Protected Sub Bulk_Insert(sender As Object, e As EventArgs)
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(1) {New DataColumn("CustomerId", GetType(Integer)), New DataColumn("CompanyName", GetType(String))})
For Each row As GridViewRow In GridView1.Rows
Dim CustomerId As Integer = Integer.Parse(row.Cells(1).Text)
Dim CompanyName As String = row.Cells(2).Text
dt.Rows.Add(CustomerId, CompanyName)
Next
If dt.Rows.Count > 0 Then
Dim consString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(consString)
Using sqlBulkCopy As New SqlBulkCopy(con)
sqlBulkCopy.DestinationTableName = "destination2"
sqlBulkCopy.ColumnMappings.Add("CustomerId", "CustomerId")
sqlBulkCopy.ColumnMappings.Add("CompanyName", "CompanyName")
con.Open()
sqlBulkCopy.WriteToServer(dt)
con.Close()
End Using
End Using
End If
Response.Redirect("Default.aspx")
End Sub
请任何帮助
答案 0 :(得分:0)
免责声明:我是Bulk Operations
的所有者此库不是免费的。
类似于SqlBulkCopy
,但对于PostgreSQL,该库使您可以轻松地执行批量操作,例如:
示例:
var bulk = new BulkOperation(connection);
bulk.ColumnMappings.Add("CustomerId", "CustomerId")
bulk.ColumnMappings.Add("CompanyName", "CompanyName")
bulk.BulkInsert(dt);