0

我有这个用于批量插入 CSV 文件的工作脚本。

代码是:

    ' OPEN DATABASE
    dim objConn,strQuery,objBULK,strConnection
    set objConn = Server.CreateObject("ADODB.Connection")
    objConn.ConnectionString = "Driver={SQL Server Native Client 11.0};Server=DemoSrvCld;Trusted_Connection=no;UID=dcdcdcdcd;PWD=blabla;database=demotestTST;"
    objConn.Open strConnection
    set objBULK = Server.CreateObject("ADODB.Recordset")
    set objBULK.ActiveConnection = objConn

dim strAPPPATH
strAPPPATH="C:\DEMO001Test.CSV"
    strQuery = "BULK INSERT EFS_OlderStyle FROM '" & strAPPPATH & "' WITH (firstrow=1, FIELDTERMINATOR=',', ROWTERMINATOR='\n')"
Set objBULK= objConn.Execute(strQuery) 
objConn.Close

以下是 .CSV 文件的示例:

Date,Time,Card Number,Driver Id,Driver Name,Unit No,Sub-Fleet,Hub Miles,Odo Miles,Trip No,Invoice,T/S Code,In Dir,T/S Name,T/S City,ST,Total Inv,Fee,PPU,Fuel_UOM,Fuel_CUR,RFuel_UOM,RFuel_CUR,Oil_CUR,Add_CUR,Cash Adv,Tax,Amt Billed,Svc Bill,Chain,Ambest,MPU
10/08/13,03:20,70113531460800693,,,2100,,,,,0454591156,546200,Y,PILOT QUARTZSITE 328,QUARTZSITE,AZ,742.30,1.00,3.749,149.000,558.60,49.00,183.70,0.00,0.00,0.00,0.00,743.30,S, ,N,0.0
10/08/13,07:03,70110535170800735,,,6210,,,,,343723,512227,Y,PETRO WHEELER RIDGE,LEBEC,CA,678.78,1.00,4.169,139.140,580.08,23.68,98.70,0.00,0.00,0.00,0.00,679.78,S, ,N,0.0

但是我现在拥有的 .CSV 文件与上面的不同。

以下是当前 .CSV 文件的示例:

"BRANCH","CARD","BILL_TYPE","AUTH_CODE","INVOICE","UNIT","EMP_NUM","TRIP","TRAILER","HUB/SPEED","VEH_LICENSE","DRIVER","DATE","TIME","CHAIN","IN_NETWORK","TS#","TS_NAME","TS_CITY","TS_STATE","PPG","NET_PPG","FUEL_GALS","FUEL_AMT","RFR_GALS","RFR_AMT","CASH","MISC","INV_TOTAL","FEE","DISC","INV_BALANCE",1.00,1.00,"E","004ACS","02812","365","-","-","0",0.00,"-","JOHN S  ",11/4/2013,"16:18:49E","IC","N",3257.00,"IRVING HOULTON","HOULTON","ME",3.95,3.95,121.57,480.08,0.00,0.00,0.00,0.00,480.08,1.50,0.00,481.58
"BRANCH","CARD","BILL_TYPE","AUTH_CODE","INVOICE","UNIT","EMP_NUM","TRIP","TRAILER","HUB/SPEED","VEH_LICENSE","DRIVER","DATE","TIME","CHAIN","IN_NETWORK","TS#","TS_NAME","TS_CITY","TS_STATE","PPG","NET_PPG","FUEL_GALS","FUEL_AMT","RFR_GALS","RFR_AMT","CASH","MISC","INV_TOTAL","FEE","DISC","INV_BALANCE",1.00,2.00,"E","014ACI","976234","430","-","-","0",0.00,"-","STACY    ",11/4/2013,"00:21:16E","F","Y",8796.00,"PILOT 405","TIFTON","GA",3.77,3.77,172.65,650.73,0.00,0.00,0.00,0.00,650.73,1.50,0.00,652.23

我编辑了 ms sql 数据库字段以反映新的 .csv 字段,但新旧 .csv 文件不存储信息。以同样的方式。如何解决此问题以使其正常工作?

我在想先删除所有的",然后删除除一个以外的所有"BRANCH","CARD","BILL_TYPE","AUTH_CODE","INVOICE","UNIT","EMP_NUM","TRIP","TRAILER","HUB/SPEED","VEH_LICENSE","DRIVER","DATE","TIME","CHAIN","IN_NETWORK","TS#","TS_NAME","TS_CITY","TS_STATE","PPG","NET_PPG","FUEL_GALS","FUEL_AMT","RFR_GALS","RFR_AMT","CASH","MISC","INV_TOTAL","FEE","DISC","INV_BALANCE",

然后保存 .csv 文件,然后重新打开它。

但我认为/希望还有另一种方式?请帮忙...谢谢。

4

1 回答 1

0

当然:有几种方法可以解决这个问题。适合您的解决方案将取决于您必须投入的时间和精力来解决这个问题,以及这是一次性导入还是您想要简化的流程。

几个解决方案:

1. 将 CSV 文件的格式更改为类似于旧版本。

这可以很容易地完成:

  • 下载notepad++之类的文本编辑器
  • 在此编辑器中打开您的 CSV 文件
  • 执行查找/替换操作:

"BRANCH","CARD","BILL_TYPE","AUTH_CODE","INVOICE","UNIT","EMP_NUM","TRIP","TRAILER","HUB/SPEED","VEH_LICENSE","DRIVER", "DATE","TIME","CHAIN","IN_NETWORK","TS#","TS_NAME","TS_CITY","TS_STATE","PPG","NET_PPG","FUEL_GALS","FUEL_AMT"," RFR_GALS","RFR_AMT","CASH","MISC","INV_TOTAL","FEE","DISC","INV_BALANCE"

用。。。来代替: ””

  • 最后将上面的行添加为您的标题 - 以与旧文件相同的格式快速重新格式化您的新文件。

    • 注意:如果您有一次性导入,这可能是最佳选择。

2. 以编程方式在您的代码中进行上述更改

由于每行的开头包含您希望忽略的字段,因此您可以根据字符数轻松截断每行。String.Replace函数可用于在插入数据库之前用 String.Empty 替换行的初始(可忽略)部分。

于 2013-11-11T23:16:40.010 回答