关闭边栏(ESC)打开边栏(ESC)
最近公司在处理盘点报告的时候,需要取ERP里的数据,就做了一个工具,能生成这个月盘点的店所有的盘点取数数据:包括帐面金额、盘亏金额、盘点表ID,这里的金额已经是按总部要求按ABC之类的。因为在导入8800016数据时,数据量比较大,记录数有二十几万条以上,只能导出TXT格式。下面是对TXT格式导入SQL的一些操作
一、导入TXT时如果数据库已存在指定表的时候用BULK Insert:
conn.Execute (“BULK Insert [指定表] From ‘” + CommonDialog1.FileName + “‘ WITH (FIELDTERMINATOR =’ ‘,ROWTERMINATOR = ‘\n’)”)
二、导入的TXT如果是要求生成新表的时候:
sql_txt = “select * into 代码_活动商品 from OpenRowset(‘MSDASQL’, ‘Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=D:\;’,’select * from tmpdb.txt’)”
下面是在VB6.0里操作导入的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
'导入到指定表 If check_qx_f(shop_num, "盘点权限") = True Then Dim lngRecsAff As Long CommonDialog1.FileName = "" CommonDialog1.Flags = 4096 CommonDialog1.Filter = "所有文件(*.*)|*.*|Text(*.txt)|*.txt" CommonDialog1.FilterIndex = 2 CommonDialog1.DialogTitle = "选择要导入的源文件" CommonDialog1.Action = 1 If CommonDialog1.FileName = "" Then MsgBox "没有选择文件,请重新选择要导入的文件", vbOKOnly + vbExclamation, "信息提示" Exit Sub End If If GetFileExtension(CommonDialog1.FileName) <> "TXT" Then MsgBox "导入的文件格式不是有效的TXT文件,请重新选择文件!", vbOKOnly + vbExclamation, "信息提示" Exit Sub End If DoEvents lf.Show lf.s_txt.Caption = "正在导入TXT格式分店库存..." If Not conn.Execute("select name from sysobjects where name='vb_pd_kc'").EOF Then conn.Execute ("drop table vb_pd_kc") End If '创建一个表用来放TXT文件 conn.Execute ("create table vb_pd_kc(药品编号 varchar(50),数量 decimal(18,2))") '将TXT文件导入到这个表里,用了BULK,分隔符是空格,回车结束,时空导出的数据要删除最后一行 conn.Execute ("BULK Insert vb_pd_kc From '" + CommonDialog1.FileName + "' WITH (FIELDTERMINATOR =' ',ROWTERMINATOR = '\n')"), lngRecsAff, adExecuteNoRecords conn.Execute ("导入帐面数量 '" + kc_index + "','" + pd_tables + "'") yymx_cx_Click Unload lf End If '导入生成新的表: If check_qx_f(shop_num, "活动代码") = True Then Dim lngRecsAff As Long Dim sql_txt As String CommonDialog1.FileName = "" CommonDialog1.Flags = 4096 CommonDialog1.Filter = "所有文件(*.*)|*.*|Text(*.txt)|*.txt" CommonDialog1.FilterIndex = 2 CommonDialog1.DialogTitle = "选择要导入的源文件" CommonDialog1.Action = 1 If CommonDialog1.FileName = "" Then MsgBox "没有选择文件,请重新选择要导入的文件", vbOKOnly + vbExclamation, "信息提示" Exit Sub End If If GetFileExtension(CommonDialog1.FileName) <> "TXT" Then MsgBox "导入的文件格式不是有效的TXT文件,请重新选择文件!", vbOKOnly + vbExclamation, "信息提示" Exit Sub End If DoEvents lf.Show lf.s_txt.Caption = "正在导入TXT格式文件..." If Not conn.Execute("select name from sysobjects where name='代码_活动商品'").EOF Then conn.Execute ("drop table 代码_活动商品") End If sql_txt = "select * into 代码_活动商品 from OpenRowset('MSDASQL', 'Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=" + Left(CommonDialog1.FileName, Len(CommonDialog1.FileName) - Len(GetFileName(CommonDialog1.FileName))) + ";','select * from " + GetFileName(CommonDialog1.FileName) + "')" conn.Execute (sql_txt), lngRecsAff, adExecuteNoRecords Unload lf MsgBox "TXT格式文件成功,本次共导入" & lngRecsAff & "条记录!", vbOKOnly + vbExclamation, "信息提示" End If |