Foxtable(狐表)用户栏目专家坐堂 → [分享] 数据快速保存


  共有11681人关注过本帖平板打印复制链接

主题:[分享] 数据快速保存

帅哥哟,离线,有人找我吗?
lsy
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:5246 积分:33163 威望:0 精华:8 注册:2013/1/17 21:28:00
[分享] 数据快速保存  发帖心情 Post By:2014/6/24 14:23:00 [只看该作者]

先感谢Bin、jspta、有点甜、逛逛

 

狐表什么都好,就是最后一步的保存,不够快。

 

修改或新增的数据,如果在一百行之内,还是狐表的代码简单、方便,也不会有速度慢的感觉。

但是一旦修改或新增的数据记录,超过千条,速度差距,就能明显感觉到。

 

有的项目,需要经常导入、合并同一张表,数据量也很大,保存是个难关。

 

ACCESS数据源,好多SQL语句不支持,只能在SQL数据源中测试了。

 

订单表,两千行数据。

 

一、Filler或高速合并,然后DataTables("订单").Load,200

Dim dt As Date = Date.Now
If DataTables.Contains("导入订单") AndAlso DataTables.Contains("订单") Then
    Dim f As New Filler
    f.SourceTable = DataTables("导入订单")
    f.DataTable = DataTables("订单")
    f.ExcludeExistValue = True
    f.Filter = DataTables("导入订单").LoadFilter
    f.Fill()
End If
DataTables("订单").Load
Dim tp As TimeSpan
tp = Date.Now - dt
MessageBox.Show(tp.TotalSeconds)

二、逐行插入 100

Dim dt As Date = Date.Now
Dim cmd As New SQLCommand
cmd.C
Dim sql1,sql2 As String
For Each c As Col In Tables("导入订单").Cols
    sql1 + = c.Name & ","
Next
For Each r As Row In Tables("导入订单").Rows
    sql2 = ""
    For Each c As Col In Tables("导入订单").Cols
        If c.IsDate Then
            sql2 + = "'" & r(c.Name) & "'" & ","
        ElseIf c.IsString Then
            sql2 + = "'" & r(c.Name) & "',"
        ElseIf c.IsBoolean Then
            If r(c.Name) = True Then
                sql2 + = 1 & ","
            ElseIf r(c.Name) = False Then
                sql2 + = 0 & ","
            End If
        Else
            sql2 + = r(c.Name) & ","
        End If
    Next
    cmd.CommandText = "InSert Into 订单 (" & sql1.Trim(",") & ") Values (" & sql2.Trim(",") & ")"
    cmd.ExecuteNonQuery
Next
DataTables("订单").Load
Dim tp As TimeSpan
tp = Date.Now - dt
MessageBox.Show(tp.TotalSeconds)

三、多行一次插入,用分号(;)分隔语句 12

Dim dt As Date = Date.Now
Dim cmd As New SQLCommand
cmd.C
Dim sql1,sql2 As String
For Each c As Col In Tables("导入订单").Cols
    sql1 + = c.Name & ","
Next
For Each r As Row In Tables("导入订单").Rows
    sql2 = ""
    For Each c As Col In Tables("导入订单").Cols
        If c.IsDate Then
            sql2 + = "'" & r(c.Name) & "'" & ","
        ElseIf c.IsString Then
            sql2 + = "'" & r(c.Name) & "',"
        ElseIf c.IsBoolean Then
            If r(c.Name) = True Then
                sql2 + = 1 & ","
            ElseIf r(c.Name) = False Then
                sql2 + = 0 & ","
            End If
        Else
            sql2 + = r(c.Name) & ","
        End If
    Next
    cmd.CommandText + = "InSert Into 订单 (" & sql1.Trim(",") & ") Values (" & sql2.Trim(",") & ");"   
Next
cmd.CommandText = cmd.CommandText.Trim(";")
cmd.ExecuteNonQuery
DataTables("订单").Load
Dim tp As TimeSpan
tp = Date.Now - dt
MessageBox.Show(tp.TotalSeconds)

四、多行一次插入,用 Union All 分隔语句 8

Dim dt As Date = Date.Now
Dim cmd As New SQLCommand
cmd.C
Dim sql1,sql2 As String
For Each c As Col In Tables("导入订单").Cols
    sql1 + = c.Name & ","
Next
For Each r As Row In Tables("导入订单").Rows
    sql2 = ""
    For Each c As Col In Tables("导入订单").Cols
        If c.IsDate Then
            sql2 + = "'" & r(c.Name) & "'" & ","
        ElseIf c.IsString Then
            sql2 + = "'" & r(c.Name) & "',"
        ElseIf c.IsBoolean Then
            If r(c.Name) = True Then
                sql2 + = 1 & ","
            ElseIf r(c.Name) = False Then
                sql2 + = 0 & ","
            End If
        Else
            sql2 + = r(c.Name) & ","
        End If
    Next
    cmd.CommandText + = "Select " & sql2.Trim(",") & vbcrlf & "Union All" & vbcrlf
Next
cmd.CommandText = "InSert Into 订单 (" & sql1.Trim(",") & ")" & vbcrlf & cmd.CommandText.SubString(0,cmd.CommandText.Length - 11)
cmd.ExecuteNonQuery
DataTables("订单").Load
Dim tp As TimeSpan
tp = Date.Now - dt
MessageBox.Show(tp.TotalSeconds)


 回到顶部
总数 25 1 2 3 下一页