Objective
The latest projects need to deal with DBF files often, in the actual scenario, many software needs to interact with some old systems, and these systems are using the FoxPro database, reading DBF files are generally divided into two situations: first; The driver of the installation FoxPro is read, second; no driver is installed, Use ODBC for reading.
Specifically how to set the Dbf/foxpro connection string, you can refer to this article (https://www.connectionstrings.com/dbf-foxpro/)
Scenario One: Installing the FoxPro driver
You can download the file "Vfpoledbsetup.msi" for installation at Microsoft Official website (https://www.microsoft.com/en-us/download/details.aspx?id=14839). Then use code to read as follows:
1 stringFilePath =@"C:\Temp\test.dbf";//file path, such as: C:\TEMP\TEST.DBF2FileInfo FileInfo =NewFileInfo (filePath);3 stringDirectoryName =Fileinfo.directoryname; //Folder directory 4 stringFileName =Fileinfo.name;5 6OleDbConnection conn =NewOleDbConnection ();7 stringConnStr =@"Provider=vfpoledb.1;data source="+ DirectoryName +"; Collating Sequence=machine";8Conn. ConnectionString =connstr;9 Conn. Open ();Ten One stringstrSQL =@"SELECT * from"+FileName; AOleDbDataAdapter da =NewOleDbDataAdapter (strSQL, conn); -DataTable dt =NewDataTable (); -Da. Fill (DT);
Scenario Two: Do not install drivers, use ODBC to read
1 Private voidButton1_Click (Objectsender, EventArgs e)2 {3 Try4 {5 stringDirectoryPath =@"C:\Temp";//the stored dbf folder directory. 6 stringFileName =@"test.dbf";//dbf file name, here is TEST.DBF because this is the name of the table, so the suffix. DBF can be omitted, directly test can be. 7 stringstrconn =@"driver={microsoft DBASE Driver (*.dbf)};D riverid=277;dbq="+DirectoryPath;8 9System.Data.Odbc.OdbcConnection Odbcconn =NewSystem.Data.Odbc.OdbcConnection ();TenOdbcconn.connectionstring =strconn; One Odbcconn.open (); A - stringstrSQL =@"SELECT * from"+FileName; - theOdbcDataAdapter ODA =NewOdbcDataAdapter (strSQL, odbcconn); -DataTable dt =NewDataTable (); - ODA. Fill (DT); - +Datagridview1.datasource =DT; - odbcconn.close (); + } A Catch(Exception ex) at { - MessageBox.Show (ex. Message); - } -}
ODBC Read exception problem
In the actual process, we often encounter a problem that prompts the Microsoft Jet database engine to find no object .
But! Detected n times, the name and path are not wrong, the internet also looked up a lot of relevant information, but have not been resolved (the problem is the same, but the solution is not valid). After repeated testing, finally found the problem:DBF file name can not exceed 8 characters, once more than 8 characters, will report such a mistake. (Why is it so unclear ~%>_<%)
Workaround: Control the DBF file name to within 8 characters, temporarily unable to find a better method
PS: If in doubt, please leave a message, without permission, may not be reproduced privately, reproduced please indicate the source: http://www.cnblogs.com/xuliangxing/p/7690709.html
C # resolves an issue where the Microsoft Jet database engine cannot find objects by reading the DBF file