C# Read from .DBF files into a datatable - c#

I need to connect to a .dbf file in visual Studio using C# and populate a data table. Any ideas? I can currently view the tables in Visual Fox Pro 9.0
Code I have tried and failed, keep getting
External table is not in the expected format.
private OleDbConnection conn;
private OleDbCommand cmd;
private OleDbDataReader dr;
private string sqlStr = "";
private DataSet myDataSet;
private OleDbDataAdapter myAdapter;
void test2()
{
conn = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Users\\PC1\Documents\\Visual FoxPro Projects\\;Extended Properties=DBASE IV;");
conn.Open();
sqlStr = "Select * from Clients.dbf";
//Make a DataSet object
myDataSet = new DataSet();
//Using the OleDbDataAdapter execute the query
myAdapter = new OleDbDataAdapter(sqlStr, conn);
//Build the Update and Delete SQL Statements
OleDbCommandBuilder myBuilder = new OleDbCommandBuilder(myAdapter);
//Fill the DataSet with the Table 'bookstock'
myAdapter.Fill(myDataSet, "somename");
// Get a FileStream object
FileStream myFs = new FileStream
("myXmlData.xml", FileMode.OpenOrCreate, FileAccess.Write);
// Use the WriteXml method of DataSet object to write XML file from the DataSet
// myDs.WriteXml(myFs);
myFs.Close();
conn.Close();
}

This code worked for me!
public DataTable GetYourData()
{
DataTable YourResultSet = new DataTable();
OleDbConnection yourConnectionHandler = new OleDbConnection(
#"Provider=VFPOLEDB.1;Data Source=C:\Users\PC1\Documents\Visual FoxPro Projects\");
// if including the full dbc (database container) reference, just tack that on
// OleDbConnection yourConnectionHandler = new OleDbConnection(
// "Provider=VFPOLEDB.1;Data Source=C:\\SomePath\\NameOfYour.dbc;" );
// Open the connection, and if open successfully, you can try to query it
yourConnectionHandler.Open();
if (yourConnectionHandler.State == ConnectionState.Open)
{
string mySQL = "select * from CLIENTS"; // dbf table name
OleDbCommand MyQuery = new OleDbCommand(mySQL, yourConnectionHandler);
OleDbDataAdapter DA = new OleDbDataAdapter(MyQuery);
DA.Fill(YourResultSet);
yourConnectionHandler.Close();
}
return YourResultSet;
}

Visual FoxPro DBFs are NOT dBase IV DBFs, and as such are unreadable by most versions of Microsoft Access's Jet database engine. (MSDN has some specifics, if you care.)
You'll need to either export the DBF from FoxPro into an actual dBase format, or you'll need to have C# open it using the Visual FoxPro OLEDB provider.
Once you have the provider installed, you'll need to change the "Provider" argument of your connection string to the following, assuming your DBF is in that folder.
Provider=VFPOLEDB.1;Data Source=C:\Users\PC1\Documents\Visual FoxPro Projects\;
(Use an #"" string format; you missed a slash in the code sample, between PC1 and Documents.)

Related

Fill data table using access - C#

I built a connection string to access data from a database to fill a datatable.
connectionname = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=d:\\projects\\checking.mdb; OLE DB Services=-1"
DataTable results = new DataTable();
using (OleDbConnection thisConnection = new OleDbConnection(connectionname))
{
OleDbCommand cmd = new OleDbCommand("SELECT * from PatTestTable", thisConnection);
thisConnection.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(results);
}
There is no error and in the debug mode, I can see the connection string being built correctly but the results datatable is empty. Did I miss any step?

Syntax error in From clause (easiest from clause possible)

I have an error in my easiest From clause (ErrorCode: -2147217900) and I do not know why...
Here my Code:
static string ConnString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=E:\\P-OT-MT\\P-OT-DB.accdb; Jet OLEDB:Database Password=*************;";
public static DataSet DS_USERS;
public static void INIT_DS()
{
// Initialize the USERS dataset and write the database information to it
DS_USERS = new DataSet();
string SQL = "SELECT * FROM USER;";
using (OleDbConnection Conn = new OleDbConnection(ConnString))
{
Conn.Open();
OleDbCommand cmd = new OleDbCommand(SQL, Conn);
OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);
adapter.Fill(DS_USERS);
cmd.Dispose();
adapter.Dispose();
Conn.Close();
}
}
I dont get where the error is... the Table USER is existant and the location of the Database is also correct... The password is correct too...
I hope you can help me
USER is a reserved word in MS Access.
See: List of reserved words in Access 2002 and in later versions of Access
You have to escape the word using [].
Use: string SQL = "SELECT * FROM [USER];";

inserting xls file values into the odbc database

i want to insert my xls file sheet into the ODBC server in c# .....kindly help me out with its c# coding....I can only find sql server related problems in the internet as i want to work in ODBC.
this is my coding :-
string ConnectionString, str;
OdbcConnection con;
OdbcCommand cmd;
SqlBulkCopy bkcp ;
private void btnSend_Click(object sender, EventArgs e)
{
string ConnectionString = "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=ak_db.excel_table;Uid=root;Pwd=root";
using (OdbcCommand cmd = new OdbcCommand(ConnectionString))
{
OdbcCommand command = new OdbcCommand("Select * FROM [Sheet1$]", con);
con.Open();
// Create OdbcDataReader to Data Worksheet
using (OdbcDataReader dr = cmd.ExecuteReader())
{
// SQL Server Connection String
string OdbcConnectionString = "Driver={MySQL ODBC 3.51 Driver};Server=localhost;Database=ak_db.excel_table;Uid=root;Pwd=root";
}
i dont know what to do after this ...so kindly help me out....
One way to go is to read the values from your excel file and store them in a dataset like described here:
Reading-Excel-Files-From-C-Sharp
and then writing this dataset to your online database:
saving-dataset-to-database
Good Luck!

Error when opening dbf file: External table is not in expected format

I'm using the code below to import a dbf file.
But I am getting an error:
'External table is not in expected format'
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];
}
Any help?
Same problem (on 64Bit Systems), solution
Download: http://download.microsoft.com/download/b/f/b/bfbfa4b8-7f91-4649-8dab-9a6476360365/VFPOLEDBSetup.msi
string connString = #"Provider=vfpoledb;Data Source=C:\Directory;Collating Sequence=machine;";
using (OleDbConnection con = new OleDbConnection(connString))
{
con.Open();
OleDbCommand command = new OleDbCommand("Select * from Table.DBF", con);
OleDbDataReader reader = command.ExecuteReader();
...
}
When dealing with data sources for dBase/FoxPro, the source typically refers to EITHER a Database name, or just the path.
Once that is opened, you can just do
select * from YourTable
(no .dbf file name suffix required in the query. It will resolve itself by looking in the path or the connected database).

Excel file - It is already opened exclusively by another user,

I am reading the excel file using C# and below is the code which is working as expected EXCEPT that every time i run the app, I have to close the excel file otherwise I get the below error message:
The Microsoft Access database engine cannot open or write to the file ''. It is already opened exclusively by another user, or you need permission to view and write its data..
my question is: is there a way i close the excel file once i am done reading?
public static DataTable LoadExcelWorkbook(string workbookName)
{
OleDbConnection connection;
string connectionString = String.Format(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", EXCELFILENAME);
string query = String.Format("select * from [{0}$]", workbookName);
using(OleDbConnection conn = new OleDbConnection(connectionString))
{
connection = new OleDbConnection(connectionString);
connection.Open();
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
DataTable myTable = dataSet.Tables[0];
dataAdapter.Dispose();
connection.Close();
dataSet.Dispose();
//CLOSE THE EXCEL FILE?????????
if (myTable != null)
return myTable;
return null;
}
}
use sheet1 name instead of workbook name

Categories