Bulkcopy(三),将VFP的cursor导入MSSQL

*--需要WebView2.DLL V.105版以上支持
*--如: 将VFP的cursor导入MSSQL
*--如:SQL服务器在本机
*--要使用本DemO需要在MSSQL建立表“test” 目录:\testdata\test.sql 有表结构脚本

Clear 

Local lcstrOledbConn       ; 
    , lcstrSQLServertConn  ;
    , lcSql                   ;
    , lcFilename           ;
    , lnTimeOut               ;
    , lisIdentity            ;
    , lnBatchSize           ;
    , lnRowcount           ;
    , lnColCount            ;
    , lcErrorMsg           ;
    , lntimeStart          ;
    , loNetFunction        ;
    , lcPath 

m.lntimeStart           = Seconds()    
m.lcstrSQLServertConn   = [uid=sa; pwd=1234567;Database=autoparts;Server=127.0.0.1;Connect Timeout = 50; Max Pool size=1024;Min pool Size=5]
m.lnTimeOut             = 60
m.lnBatchSize            = 10000
m.lisIdentity             = .F.
m.lnRowcount            = 0
m.lnColCount             = 0
m.lcErrorMsg            = ""
            

If _vfp.StartMode = 0 Then 
   m.lcPath = Addbs( Justpath( _vfp.ActiveProject.Name  ))
Else 
   m.lcPath = Addbs( Justpath( sys(16,0) )  )
Endif  

If File( 'testdata\test.dbf' ) Then 
   If !Used( 'test' ) Then 
       Use testdata\test.dbf In 0 Alias test 
   Endif 
   
Endif 

m.loNetFunction  = Createobject( "Netsuite.Function")

*--初始化DLL中的DT对象(这个对象用来装VFP的cursor数据)
*--有关说明看类方法说明
m.lntimeStart1 = Seconds()    
If !m.loNetFunction.sqlbulkdtinit( 'test'                                             ; &&MSSQL表名
                                 , [id,xdate,bdsna,pano,pana,orig,md,jj1,jj2,issta] ; &&字段序列
                                 , m.lcstrSQLServertConn                            ; &&SQL配置连接串
                                 , @lcErrorMsg ) Then                                   &&错误信息
    Messagebox( m.lcErrorMsg  ,  16 , 'sqlbulkdtinit失败!' )
    Return .F. 
Endif 
?'DLL实始化Datatable耗时:' + Transform( Seconds()    - m.lntimeStart1 )


*--cursor的记录以 lcDelimited 为分隔符写入DLL的Datatable
   *注意1 字段名称区分大小写
   *注意2 C 类型字段     字段值不要与分隔符相同,要不然分隔不了 必要时请使用 STRTRAN() 处理掉 
   *注意3 字段宽度
   *注意4 大数据要分段,不要全部压入DLL内存,在网络上最好10000一个打包(实际情况而定)
   
Local lcDelimited ;
     , lcData       ;
     , lntimeStart2   
 
m.lcDelimited  = Chr(0)+ Chr(9) + Chr(0) &&注意这个分隔符它是分隔字段列值的
m.lntimeStart2 = Seconds()    

Select test 
Scan
 
    m.lcData = Transform( test.id ) + m.lcDelimited + ;     &&ID        IDentity 
               Ttoc( Datetime())    + m.lcDelimited + ;  &&xdata     日期时间类型
               Alltrim( test.bdsna )+ m.lcDelimited + ;     &&bdsna    字符串
                  Alltrim( test.pano  )+ m.lcDelimited + ;     &&pano        字符串
                  Alltrim( test.pana  )+ m.lcDelimited + ;  &&pana        字符串
                  Alltrim( test.orig  )+ m.lcDelimited + ;     &&orig        字符串
                  Alltrim( test.md    )+ m.lcDelimited + ;     &&md        字符串
                  '2.08'                 + m.lcDelimited + ;  &&jj1         数值类型
                  '1.00'                + m.lcDelimited + ;  &&jj2         数值类型    
                  'false'                                     &&issta     逻辑类型

    If !m.loNetFunction.sqlbulkAddData( m.lcData       ;     &&    导入数据
                                      , m.lcDelimited ;  &&    分隔符
                                      ,    @lcErrorMsg ) Then 
        Exit 
    Endif 
    
Endscan                          

If !Empty( m.lcErrorMsg ) Then 
   Messagebox( m.lcErrorMsg  ,  16 , 'sqlbulkAddData失败!' )
   Return .F.
Else 
   ?'往DLL插入内存数据耗时:' + Transform( Seconds()    - m.lntimeStart2 )
Endif 


Local lntimeStart3 
m.lntimeStart3 = Seconds()    

If m.loNetFunction.cursorbulkinsert ( 'test'                ;  &&SQL导入的表名
                                     , m.lnTimeOut             ;  &&超时时间
                                     , m.lnBatchSize         ;  &&多少条一个批次
                                     , m.lisIdentity          ;  &&是否导入到SQL表时重新生成ID
                                     , m.lcstrSQLServertConn;  &&与SQLServer连接配置信息    
                                     , @lnRowcount            ;  &&成功导入多少行
                                     , @lnColCount             ;  &&成功导入多少列
                                     , @lcErrorMsg            )  &&错误信息
                                     
    ?'DLL的DataTable提交给MSSQL耗时:' + Transform( Seconds()    - m.lntimeStart3   )                

    Messagebox( '导入MSSQL整体耗时:'  + Transform( Seconds()    - m.lntimeStart   ) + '记录数:' + Transform( m.lnRowcount    ) , 64 )

Else 
    Messagebox( m.lcErrorMsg , 16 , 'cursorbulkinsert失败' )
Endif 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值