Share via


code for import .dbf file into sqlserver using C#

Question

Tuesday, February 19, 2008 1:14 AM

hi

i want c# code for  import .dbf file into sqlserver using C#

pls help

 

thanx

All replies (3)

Tuesday, February 19, 2008 9:25 AM âś…Answered

i want c# code for  import .dbf file into sqlserver using C#

 

Have you considered SSIS (SQL Server Integration Services)?

Otherwise write code to read from one and write into the other (with LINQ to SQL this is a lot easier than it used to be). 


Saturday, April 26, 2008 11:09 AM

See code below I used to dump an airport database file in .dbf into a sql database ...  designed for temporary use on my local server only ...

 

public void readAirportDBF()

{

//define the connections to the .dbf file

OleDbConnection oConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\webprojects\airports;Extended Properties=dBase III");

OleDbCommand command = new OleDbCommand("select SITE_NO, LOCID, CITY_NAME, STATE_NAME, LATITUDE, LONGITUDE from airports.dbf", oConn);

//open the connection and read in all the airport data from .dbf file into a datatable

oConn.Open();

DataTable dt = new DataTable();

dt.Load(command.ExecuteReader());

oConn.Close();  //close connection to the .dbf file

//create a reader for the datatable

DataTableReader reader = dt.CreateDataReader();

myConnection.Open();   ///this is my connection to the sql server

SqlBulkCopy sqlcpy = new SqlBulkCopy(myConnection); 

sqlcpy.DestinationTableName = "Airports";  //copy the datatable to the sql table

sqlcpy.WriteToServer(dt);

myConnection.Close();

reader.Close();

}


Thursday, June 23, 2011 3:31 PM

I've inserted the following code into a project where I am trying to transfer DBF files into SQL.  I keep getting an error at  dt.Load(command.ExecuteReader());

 

The error:

System.Data.OleDb.OleDbException was caught
  Message=IErrorInfo.GetDescription failed with E_FAIL(0x80004005).
  Source=System.Data
  ErrorCode=-2147467259
  StackTrace:
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
       at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
       at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
       at System.Data.OleDb.OleDbCommand.ExecuteReader()
       at HotFolderWatch.HotFolderWatch.readAirportDBF() in C:\Users\nicole.karek\Documents\Visual Studio 2010\Projects\HotFolderWatch4\HotFolderWatch\HotFolderWatch.cs:line 224
       at HotFolderWatch.HotFolderWatch.btnTransfer_Click_1(Object sender, EventArgs e) in C:\Users\nicole.karek\Documents\Visual Studio 2010\Projects\HotFolderWatch4\HotFolderWatch\HotFolderWatch.cs:line 274
  InnerException:

public void readAirportDBF()

        {        

//define the connections to the .dbf file        

OleDbConnection oConn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\share\hotfolder\Extended Properties=DBase III");

OleDbCommand command = new OleDbCommand("select Last from Input.dbf", oConn);

//open the connection and read in all the airport data from .dbf file into a datatable

DataTable dt = new DataTable();

**            dt.Load(command.ExecuteReader());**

            oConn.Close(); 

//close connection to the .dbf file

//create a reader for the datatable

DataTableReader reader = dt.CreateDataReader();

            myConnection.Open();  ///this is my connection to the sql server

SqlBulkCopy sqlcpy = new SqlBulkCopy(myConnection);

            sqlcpy.DestinationTableName = "Airports";  //copy the datatable to the sql table

            sqlcpy.WriteToServer(dt);

            myConnection.Close();

            reader.Close();

        }

 

Any help would be appreciated!  Thanks, Nicole.