2012年10月31日水曜日

CSVファイルの取り込み後のデータをDBに追加するには


call DbOpen  'DB接続モジュール

  MySQL = "SELECT * FROM テーブル名"
  MyREC.Open MySQL, MyRDB, 3, 3, 1

  Set fs = CreateObject("Scripting.FileSystemObject")
  filename=server.mappath("../k_mem/" & newf1)
  Set readfile=fs.OpenTextFile(filename,1,False)

  Do while not readfile.atendofstream
    count=count+1
     if count>1 then
      Text=readfile.readline
       data = split(Text, ",")   'CSV ファイルは,で分割されている前提
       MyREC.addnew
      MyREC(列名A)=right(100000000000+data(2),10)  'CSV3要素目のデータで桁数併せ                                       のため0で穴埋め
      MyREC("列名B")=data(3)               '4要素目のデータ
      MyREC("列名C")="01"                 'CSVから取り込まず、固定値
      MyREC.update
      dummy=left(data(1),10)
    end if
  loop
  readfile.close
  set readfile=nothing
  set fs=nothing

call c_DbClose()  'DB切断モジュール

★例: Split(text ,",",3,1) カンマ区切りデータの3要素をテキスト形式で比較します。

     Split(text ,",") カンマ区切りデータの全ての要素をバイナリ形式で比較します。