C#读取Visual FoxPro(*.dbf)数据并使用SqlBulkCopy插入到SqlServer 2008 R2数据表中

公司数据库从32位的SqlServer 2005升级到64位的SqlServer 2008 R2后,无法再像原来通过Link Server连接VFP同步数据,因此考虑用代码程序从VFP数据库中读取所需数据然后再插入到SqlServer数据库中,下面贴出该C#代码以备忘或供有需要者参考。
        private  void  GetDataFromVFP()
        {
             OdbcConnection  conn =  null  ;
             try
            {
                 string  strOdbcConn =  @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=D:/a/b;Exclusive=No;Collate=Machine;" ;
                 string  strInsertConn =  @"User id=a;Password=b;Database=c;Server=d;Connect Timeout=50;Max Pool size=200;Min pool Size=5"  ;
                conn =  new  OdbcConnection  ();
                conn.ConnectionString = strOdbcConn;
                conn.Open();
                 string  strTableName =  ""  ;
                 for  ( int  i = -8; i < 0; i++)
                {
                    strTableName =  "FOLIO"  +  DateTime  .Today.AddDays(i).ToString( "dd" );
                     string  sql =  string  .Format( @"select a,b,c from {0}" , strTableName);
                     OdbcDataAdapter  da =  new  OdbcDataAdapter (sql, conn);
                     DataTable  dt =  new  DataTable ();
                    da.Fill(dt);
                     SqlBulkCopy  bulkCopy =  new  SqlBulkCopy (strInsertConn);
                    bulkCopy.BulkCopyTimeout = 1800000;
                    bulkCopy.DestinationTableName =  "strTableName" ;
                    bulkCopy.WriteToServer(dt);
                }
            }
             catch
            { }
             finally
            {
                 if  (conn !=  null  )
                {
                    conn.Close();
                }
            }
        }

你可能感兴趣的:(sqlserver)