导入按钮代码如下:
此主题相关图片如下:qq截图20180803160521.png
Dim count As Integer = e.Form.Controls("cboPer").value
If count = 0 Then
msgbox("请输入一个大于0 的合适批数.")
Return
End If
Output.Show("count=" & count)
Dim allCount As Integer = e.Form.Controls("cboAllCount").value
If allCount = 0 OrElse allCount > 1000000 Then
msgbox("请输入一个大于0 的合适总数(最大100W).")
Return
End If
Output.Show("allCount=" & allCount)
Dim ccbCols As WinForm.CheckedComboBox = e.Form.Controls("ccbCols")
Dim lst As new List(of String)
If ccbCols.Text > "" Then
lst.AddRange( ccbCols.text.Split(","))
Else
lst.AddRange(new String() {"ID","CreatedTime","UserName","Phone","Account","Balance","Description"})
End If
Dim cols() As String = lst.ToArray
Dim dt As new System.Data.DataTable
Dim dlg As New OpenFileDialog '定义一个新的OpenFileDialog
dlg.Filter= "Excel2007文件|*.xlsx" '设置筛选器
If dlg.ShowDialog = DialogResult.Ok Then '如果用户单击了确定按钮
Dim d1 As Date = Date.Now
Dim constring As String = Vars("c1")
using conn As System.Data.SqlClient.SqlConnection = new System.Data.SqlClient.SqlConnection(constring)
conn.Open() '打开链接
Output.Show("打开数据库链接")
using copy = new System.Data.SqlClient.SqlBulkCopy(conn)
copy.Destinati '指定服务器上目标表的名称
Dim cls() As String = {"ID","CreatedTime","UserName","Phone","Account","Balance","Description"}
For Each cl As String In cls
copy.ColumnMappings.Add(cl,cl)
Next
copy.BatchSize = 10000
copy.BulkCopyTimeout = 60
Output.Show("开始导入数据")
Dim _execl As New ExOpenXml.ExOXExecl()
try
Dim page As Integer = Math.Ceiling(allCount / count) - 1
Dim lastcount As Integer = allCount Mod count
For i As Integer = 0 To page
Dim idx As Integer = i*count
Output.Show("idx=" & idx)
_execl.Open(dlg.FileName,True) '不需要保存文档,第二个参数设置为True
dt = _execl.ReadToDataTable( ,cols , idx , IIF(i<page OrElse lastcount=0,count,lastcount))
Output.Show(i & "readend=" & Format(Date.Now,"mm:ss.ffff"))
If dt IsNot Nothing Then
Output.Show("dt.Rows.count=" & dt.Rows.count)
copy.WriteToServer(dt)
Output.Show(i & "saveend=" & Format(Date.Now,"mm:ss.ffff"))
End If
Next
Finally
_execl.dispose
End Try
End using
End using
Dim tp As TimeSpan = now() - d1
msgbox("合并导入成功,用时: " & tp.TotalSeconds & "秒.")
GC.Collect()
End If
[此贴子已经被作者于2018/8/3 16:08:56编辑过]