-- 作者:HANZHIGANG
-- 发布时间:2023/2/14 11:01:00
--
Dim dt As DataTable = DataTables("客户管理") Dim conStr As String = "server=云服务器的ID,1433;uid=用户名;pwd=密码;database=客户管理" \' Dim tran As System.Data.SqlClient.SqlTransaction Dim conn As new System.Data.SqlClient.SqlConnection(conStr)
Try Dim st As Date = Date.Now conn.Open() tran = conn.BeginTransaction() Dim mpList As new List(of System.Data.SqlClient.SqlBulkCopyColumnMapping) For Each dc As DataCol In dt.DataCols mpList.Add( new System.Data.SqlClient.SqlBulkCopyColumnMapping(dc.Name, dc.Name)) Next Dim copy As new System.Data.SqlClient.SqlBulkCopy(conn, System.Data.SqlClient.SqlBulkCopyOptions.Default, tran) For Each mp As System.Data.SqlClient.SqlBulkCopyColumnMapping In mpList Copy.ColumnMappings.Add(mp) Next copy.DestinationTableName = dt.Name copy.BulkCopyTimeout=30 copy.BatchSize = CInt(dt.DataRows.Count/10) copy.WriteToServer(dt.bas去掉中文etable) \'你的本地DataTable,要转成.net官方的基表类型 ,这句不知道什么意思,不会改 tran.Commit() Dim res As Double = Format(dt.DataRows.Count /10000,"0.00") MessageBox.Show("批量插入成功, " & res & "W行 耗时: " & (Date.Now - st).TotalSeconds & "秒") Catch ex As exception tran.Rollback() \'返回False 执行失败! MessageBox.Show("批量插入失败,原因:" & ex.message) Finally conn.Close() \'必须关闭,否则内存会一直泄露 End Try
|