How to read from a Visual Foxpro .DBF file in C# and display in a datagridview?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi, I am trying to import Foxpro .DBF files into SQL Server. However, since I was not able to do that right away I have been trying to find a basic working solution. On the Internet I found some example code for connecting to Foxpro .DBF files
and have this data be displayed in a datagrid. The connection works fine but I get an error every time the program tries to read the datatable.
Code:
<div style="color:black; background-color:white
<pre> OdbcConnection conn = <span style="color:blue new OdbcConnection();
conn.ConnectionString =
<span style="color:#a31515 "Driver={Microsoft dBASE Driver (*.dbf)};" +
<span style="color:#a31515 "Driverid=277;" +
"Dbq=C:\share\hotfolder\";

conn.Open();

System.Data.Odbc.OdbcCommand oCmd = conn.CreateCommand();
oCmd.CommandText = <span style="color:#a31515 @"SELECT * FROM C:sharehotfolderInput.dbf";

DataTable dt = <span style="color:blue new DataTable();
dt.Load(oCmd.ExecuteReader());
conn.Close();

dataGridView1.DataSource = dt;
[/code]




I receive the following error from "dt.Load(oCmd.ExecuteReader());":
System.Data.Odbc.OdbcException was unhandled<br/>
Message=ERROR [HY000] [Microsoft][ODBC dBase Driver] External table is not in the expected format.<br/>
Source=odbcjt32.dll<br/>
ErrorCode=-2146232009<br/>
StackTrace:<br/>
at System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle, RetCode retcode)<br/>
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader, Object[] methodArguments, SQL_API odbcApiMethod)<br/>
at System.Data.Odbc.OdbcCommand.ExecuteReaderObject(CommandBehavior behavior, String method, Boolean needReader)<br/>
at System.Data.Odbc.OdbcCommand.ExecuteReader(CommandBehavior behavior)<br/>
at System.Data.Odbc.OdbcCommand.ExecuteReader()<br/>
at Read_DBF.Form1.btnClick_Click(Object sender, EventArgs e) in c:usersnicole.karekdocumentsvisual studio 2010ProjectsRead_DBFRead_DBFForm1.cs:line 37<br/>
at System.Windows.Forms.Control.OnClick(EventArgs e)<br/>
at System.Windows.Forms.Button.OnClick(EventArgs e)<br/>
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)<br/>
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)<br/>
at System.Windows.Forms.Control.WndProc(Message& m)<br/>
at System.Windows.Forms.ButtonBase.WndProc(Message& m)<br/>
at System.Windows.Forms.Button.WndProc(Message& m)<br/>
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)<br/>
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)<br/>
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)<br/>
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)<br/>
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)<br/>
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)<br/>
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)<br/>
at System.Windows.Forms.Application.Run(Form mainForm)<br/>
at Read_DBF.Program.Main() in c:usersnicole.karekdocumentsvisual studio 2010ProjectsRead_DBFRead_DBFProgram.cs:line 18<br/>
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)<br/>
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)<br/>
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()<br/>
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)<br/>
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)<br/>
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)<br/>
at System.Threading.ThreadHelper.ThreadStart()<br/>
InnerException:




I have been doing a lot of research on this topic, issue, and error but am not having any luck besides having a connection made.
Any help would be appreciated.
Again, I am trying to use C# to import Foxpro .DBF files in SQL server but I am just trying out this basic solution to see if I can read the .DBFs. Any help on this example or my main goal, it would be very appreciated thanks!

<br/>
<br/>

View the full article
 
Back
Top