Problems trying to read dbf file in C# - c#

I am trying to read a dbf file in C#, but first I had the error
System.InvalidOperationException: 'The provider' Microsoft.ACE.OLEDB.12.0 'is not registered on the local machine.'
Then I installed Microsoft Access Database Engine 2010 Redistributable and now I have the error
The Microsoft Access database engine cannot open or write to the file 'C:\Users\Emmanuel\Desktop\Proyectos\program'. It is already opened exclusively by another user, or you need permission to view and write its data.'
This is the code I'm using:
var constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;";
var sql = "select * from " + file;
var ds = new DataTable();
using (var con = new OleDbConnection(constr))
{
con.Open();
using (var cmd = new OleDbCommand(sql, con))
{
using (var da = new OleDbDataAdapter(cmd))
{
da.Fill(ds);
}
}
}
Also I've tried this but I have this error
System.Data.Odbc.OdbcException: 'ERROR [HY000] [Microsoft] [ODBC dBase Driver] The external table is not in the expected format.'
string fullPath = path + file;
string sConn = "Driver={Microsoft dBASE Driver (*.dbf)};SourceType=DBF;SourceDB=" + path + ";Exclusive=No; NULL=NO;DELETED=NO;BACKGROUNDFETCH=NO;";
using (OdbcConnection dbConn = new OdbcConnection(sConn))
{
dbConn.Open();
OdbcCommand oCmd = dbConn.CreateCommand();
oCmd.CommandText = "SELECT * FROM " + fullPath;
/* Load data to table */
DataTable dt1 = new DataTable();
dt1.Load(oCmd.ExecuteReader());
dbConn.Close();
}

Related

Oledb cannot access file fix

for a small project I need to create a DataTable filled with data from an Excelsheet. Im getting this data with using an Oledb connection. Sometimes other people are using this file on the server. So I'm getting the "The process cannot access the file because it is being used by another process"
private DataTable excelSelectStatement() {
try {
string par = this._colParameters.getParameterByName("SheetName", false, null).Value;
excelDataSet = new DataSet();
string ace12 = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + _selectionStatement + "';Extended Properties='Excel 12.0;HDR=YES;IMEX=1;';";
using (OleDbConnection connection = new OleDbConnection(ace12)) {
OleDbDataAdapter da = new OleDbDataAdapter("select * from [" + par + "$]", connection);
da.TableMappings.Add("Table", "TestTable");
da.Fill(excelDataSet);
}
}
catch (Exception e) { MessageBox.Show(e.Message); }
return excelDataSet.Tables[0];
}
Is there any way with completing this with Oledb?

The Microsoft Jet database engine cannot open the file c#

Hi I am having some issues reading a CSV file with the Jet Database engine. I get this message:
The Microsoft Jet database engine cannot open the file 'C:\Users\mikec\Desktop\'. It is already opened exclusively by another user, or you need permission to view its data.
I have permission to access this folder and file. I have also tried putting in my Visual Studio project and still get the same error. This is my code
string path = #"C:\Users\mikec\Desktop\Book1.csv";
string full = Path.GetFullPath(path);
string file = Path.GetFileName(full);
string dir = Path.GetDirectoryName(full);
//create the "database" connection string
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=\"" + dir + "\\\";"
+ "Extended Properties=\"Excel 8.0;HDR=Yes;FMT=Delimited\"";
//create the database query
string query = "SELECT * FROM " + file;
//create a DataTable to hold the query results
DataTable dTable = new DataTable();
OleDbConnection con = new OleDbConnection(connString);
//create an OleDbDataAdapter to execute the query
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, con);
try
{
//fill the DataTable
dAdapter.Fill(dTable);
}
catch (InvalidOperationException e)
{
}
I'm confused to why this is happening I've looked around the Internet and cannot find a solution.
It would be great if anyone out there could point me in the right direction.
Thanks.

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).

data import from excel facing an issue

I am importing data from MS Excel.
The code i have written is,
var ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" +
uploadfile.PostedFile.FileName + ";" + "Extended Properties=Excel 12.0;";
OleDbConnection objConn = new OleDbConnection(sConnectionString);
objConn.Open();
try
{
var objCmdSelect = new OleDbCommand("select * from [Sheet1$]", objConn);
}
and so on.
I got an error which looks very generic to me
The Microsoft Office Access database engine could not find the object 'Sheet1$'. Make sure the object exists and that you spell its name and the path name correctly
*
My worksheet name is spelled correclty
but for my confirmation, i did below code
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if(dt == null)
{
return null;
}
var excelSheets = new String[dt.Rows.Count];
int i = 0;
// Add the sheet name to the string array.
foreach(DataRow row in dt.Rows)
{
excelSheets[i] = row["TABLE_NAME"].ToString();
i++;
}
*
but i got my Data Table null.
My question is the connection is open successfully but i can't read data from the excel file.
Is there any special Authentication required.?
because i am getting the above error.
Instead if Ace.OLEDB you may try by Microsoft.Jet.OLEDB, because I faced the simillar then I switch over to Jet.OLEDB
string MyExelFile = "C:\Temp\Sample.xls";
string MyExcelSheet = "[Sheet1$]";
string StrConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + MyExelFile + ";
Extended Properties=\"Excel 8.0; HDR=Yes; IMEX=1\"";
String MySQLSelect = "select * from " + MyExcelSheet + "";
DataTable Items=new DataTable() ;
System.Data.OleDb.OleDbConnection Cn = new System.Data.OleDb.OleDbConnection();
Cn.ConnectionString = StrConn;
System.Data.OleDb.OleDbDataAdapter Da = new System.Data.OleDb.OleDbDataAdapter
(MySQLSelect, Cn);
Cn.Open();
Da.Fill(Items);
Cn.Close();
</pre>

Import excel file which is on local machine to SQL server 2005

I have a path to an Excel file which is located on my local machine and I want to import the data of the Excel file to SQL Server 2005.
I have tried this code but it gives me an error:
string sSourceConStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + textBox1 + "; Extended Properties=" + "\"Excel 8.0;HDR=YES;+\"";
string dDestConStr = #"server=severIPAddress;database=databaseName;uid=userName;password=pwd";
OleDbConnection sSourceConnection = new OleDbConnection(sSourceConStr);
using (sSourceConnection)
{
string sql = string.Format("Select * FROM [{0}]", "Sheet1$");
OleDbCommand command = new OleDbCommand(sql, sSourceConnection);
sSourceConnection.Open();
using (OleDbDataReader dr = command.ExecuteReader())
{
using (SqlBulkCopy bulkCopy = new SqlBulkCopy(dDestConStr))
{
bulkCopy.DestinationTableName = "dbo.databaseName";
bulkCopy.WriteToServer(dr);
}
}
}
Error:
OleDBException was unhandled. Failure creating file.
What would cause this and how can I resolve it?
Change textBox1 to textBox1.Text in Data Source

Categories