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

新しい .csv フィールドを反映するように ms sql データベース フィールドを編集しましたが、古いファイルと新しい .csv ファイルには情報が保存されません。同じやり方で。これが機能するように修正するにはどうすればよいですか?

"最初にすべてを削除してから、1つを除いてすべて削除することを考えていました"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 回限りのインポートか、合理化したいプロセスかによって異なります。

いくつかの解決策:

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"

と置換する: ""

  • 最後に、上記の行をヘッダーとして追加します。新しいファイルを古いファイルと同じ形式ですばやく再フォーマットします。

    • 注: 1 回限りのインポートの場合は、これが最適なオプションです。

2. プログラムでコードに上記の変更を加えます。

各行の先頭には無視したいフィールドが含まれているため、文字数に基づいて各行を簡単に切り捨てることができます。String.Replace関数を使用して、DB に挿入する前に行の最初の (無視できる) 部分を String.Empty に置き換えることができます

于 2013-11-11T23:16:40.010 に答える