将DBF文件导入C# [英] Importing a DBF file into C#

查看:83
本文介绍了将DBF文件导入C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi


我使用以下源代码将dbf(DBASE)文件导入C#数据集,并且出现错误外部表格不是预期的格式 。我错过了什么?我该怎么办?



hi
I am using the following source code to import a dbf (DBASE) file into C# dataset and it is giving the error ''External table is not in expected format''. What have I missed? What should I do?

if (ofdDBF.ShowDialog()==DialogResult.OK) 
            { 
                string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ofdDBF.FileName.Substring(0, ofdDBF.FileName.LastIndexOf("\\")) + ";Extended Properties=dBASE IV;"; 
 
                OleDbConnection conn = new OleDbConnection(connStr); 
                conn.Open(); 
 
                string cmd_string = "select * from " + ofdDBF.SafeFileName.Substring(0, ofdDBF.SafeFileName.IndexOf(".")); 
                MessageBox.Show(cmd_string); 
                OleDbDataAdapter da = new OleDbDataAdapter(cmd_string, conn); 
                DataSet ds = new DataSet(); 
                da.Fill(ds); 
                dgvImport.DataSource = ds.Tables[0]; 
 
            } 





请帮助

问候

Nirmala Saravanan



Pls Help
Regards
Nirmala Saravanan

推荐答案

我有两个解决方案合二为一。



1.是DBF文件可能不是 dBase IV ? Mircosoft 文档 [ ^ ]列出 dBase III dBase 5.0 作为其他可能的值。





2.大约10年前,我成功读取了DBF文件,使用ODBC和DBASE文件的以下 ConnectionString 。通过一个 Connection 对象读取多个DBF文件。 DBF完整路径文件名在每个 SELECT 语句中指定,不在 ConnectionString 中。我在此解决方案的最后包含了针对dBase文件的ODBC文档的引用。



下面是一些示例VB.NET代码,可以帮助您入门。



I have two "solutions" in one for you.

1. Is it possible that the DBF file is not dBase IV? The Mircosoft Documentation[^] lists dBase III and dBase 5.0 as other possible values.


2. I read DBF files successfully about 10 years ago using ODBC with the following ConnectionString for the DBASE files. Multiple DBF files are read via one Connection object. The DBF full path filename is specified in each SELECT statement not in the ConnectionString. I included references to Microsoft documentation for ODBC for dBase files at the end of this solution.

Below is some sample VB.NET code that should get you started.

Imports Microsoft.Data.Odbc
...
...	
Dim cn As OdbcConnection = New OdbcConnection()
cn.ConnectionString = "DRIVER={Microsoft dBase Driver (*.dbf)};Deleted=1"
cn.Open()

Dim obCommand As OdbcCommand
Dim strSQL As String = "SELECT ID,text_type,text_line,text_data FROM " & strPath & strSource_DBF_Filename
obCommand = New OdbcCommand(strSQL, cn)



注意:ID,text_type,text_line,text_data是我的DBF文件中的列名。



经过测试:Visual Basic .NET 2003



用于ODBC DBASE驱动程序的Microsoft文档:

Microsoft ODBC桌面数据库驱动程序 [ ^ ]

dBASE驱动程序编程注意事项 [ ^ ]

dBASE数据类型 [ ^ ]

以编程方式为dBASE驱动程序设置选项 [ ^ ]


Note: ID,text_type,text_line,text_data are columns names in my DBF file.

Tested: Visual Basic .NET 2003

Microsoft Documentation for ODBC DBASE Driver:
Microsoft ODBC Desktop Database Drivers[^]
dBASE Driver Programming Considerations[^]
dBASE Data Types[^]
Setting Options Programmatically for the dBASE Driver[^]


这篇关于将DBF文件导入C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆