Thread: FoxPro Some General Questions/Bulk insert into SQL Server

Bulk insert into SQL Server
Click


1) If you use an updatable RV or SPT set buffering to 5.
2) You can use "copy to myTransfer.txt type delimited" and bulk insert
3) You can use DTS package
4) You can use OpenRowSet() SQL server function

Last one is one of the fastest I think. Below is partial class I've used for my own upsizing (fieldnames were same):

* Sample
oTransfer = CreateObject("SQLPutData")
oTransfer.PutTheData()

Define Class SQLPutData As Session
   DataSession=2
   DBPATH = Sys(5)+Curdir()+'myLocalDatabasePath'
        LocalDBC = 'myLocalDatabase.dbc'
   SQLDBNAME = "mySQLDatabase"
   conHandle = .Null.

   Procedure Init
      This.conHandle = ;
                Sqlstringconnect("Driver=SQL server;server=(local);Integrated Security=SSPI")
      SQLExec(This.conHandle,"use ["+This.SQLDBNAME+"]")
   Endproc

   Procedure PutTheData
      With This
                   .GetDataFrom("[dbo].[mytable]","[field1],[field2],[field3]")
                   SQLDisconnect(.conHandle)
                endwith
        endproc

   Procedure GetDataFrom
      Lparameters tcSQLName,tcFields
      Local lcLocalTable, lcLocalFields, lcSQL, lnResult, ix, lcError
      Local array arrErr[1]
      lcLocalTable  = Justext(Chrtran(tcSQLName,'[]',''))
      lcLocalFields = Chrtran(tcFields,'[]','')
      Text to m.lcSQL textmerge noshow
      insert into ///m.tcSQLName////
         SELECT ///m.tcFields///
         FROM OPENROWSET('VFPOLEDB',
         '///Addbs(this.DBPATH)+this.LocalDBC///';'';'',
         'select ///m.lcLocalFields/// from ///m.lcLocalTable///')
      endtext
      lnResult = SQLExec(this.conHandle, m.lcSQL)
      If m.lnResult < 0
         AError(arrErr)
         lcError = ''
         For ix = 1 to Alen(arrErr)
            lcError = lcError + Transform(arrErr[m.ix]) + Chr(13)   
         EndFor
         MessageBox(m.lcError)
      Else
         ? m.tcSQLName
      endif
   Endproc
enddefine

Instead of /// use << or >>



or from the same thread:

* mVFPTxtFile is the text file that VFP creates.
* mSQLTxtFile is what MySQL imports.  Remeber that MySQL expects Forward
* Slashes and not Back Slashes in the path.  They both point to the same file.

copy to (mVFPTxtFile) deli with "" with tab

* Run the command to import into MySQL
mCommand = "LOAD DATA INFILE '"+ mSQLTxtFile +"' INTO TABLE " ;
+ mDbfFile ;
+ [ FIELDS TERMINATED BY '\t' LINES TERMINATED BY '\r\n' ;]
=SqlExec(mHandle, mCommand)
*



SELECT *
  FROM OPENROWSET('MSDASQL',
                  'Driver={Microsoft Visual FoxPro Driver};UID=;SourceDB=путь;SourceType=DBF;Exclusive=No;BackgroundFetch=Yes;Collate=Russian;Null=No;Deleted=No;',
                  'SELECT * FROM таблица')