I have C# WinForms application that gets information from Excel sheet and stores it to my database, my code works fine on my PC but when I changed it an Error
System.InvalidOperationException: 'The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.'
display. I solved this problem by changing the project platform from x86 to x64 but when I launch the project with x64 I can't charge many files so I have to run it on x86. Guys any solutions?
Rq: My code:
String name = "Feuil1";
String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
textBox_path.Text +
";Extended Properties='Excel 12.0 XML;HDR=YES;';";
OleDbConnection con = new OleDbConnection(constr);
OleDbCommand oconn = new OleDbCommand("Select * From [" + name + "$]", con);
con.Open();
OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
DataTable data = new DataTable();
sda.Fill(data);
dataview1.DataSource = data;
using (SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["UR2k_CS.Properties.Settings.StoreConnectionString"].ConnectionString))
{
String query = "INSERT INTO dbo.Stock (RFID,name,refrence,prix,Stockdate) VALUES (#RFID,#name,#refrence,#prix,#Stockdate)";
connection.Open();
for (int i=0;i<dataview1.Rows.Count-1;i++)
{
using (SqlCommand command = new SqlCommand(query, connection))
{
if ((dataview1.Rows[i].Cells[0].Value.ToString()!=null)&&((!checkifexist(dataview1.Rows[i].Cells[0].Value.ToString()))) &&(dataview1.Rows[i].Cells[0].Value.ToString()!=""))
{
command.Parameters.AddWithValue("#RFID", dataview1.Rows[i].Cells[0].Value.ToString());
command.Parameters.AddWithValue("#name", dataview1.Rows[i].Cells[1].Value.ToString());
command.Parameters.AddWithValue("#refrence", dataview1.Rows[i].Cells[2].Value.ToString());
command.Parameters.AddWithValue("#prix", dataview1.Rows[i].Cells[3].Value.ToString());
command.Parameters.AddWithValue("#Stockdate", DateTime.Now);
command.ExecuteNonQuery();
}
}
}
connection.Close();
}
You need to download the 32 bit driver from here microsoft downloads (I think this is the correct one) but the difficulty you may run into is that a machine cannot have both the 64bit and 32bit versions installed side by side.
Using EPPlus is a more modern approach.
Related
I have a solution where i need to read values from Excel. I am using OLEDB connection to do the same. It all works fine when i run the solution in local. But when i host it in IIS i get the JIT compiler pop up when the oledb connection code is hit. Not able to figure out the problem.
Below is the OLEDB connection code :
public static DataSet FirstColumn(string filename)
{
string filepath = filename;
string sheetName = "First";
//Oledb connection to generate excel in temporary folder on server
string connectionstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + filepath + "';Extended Properties=\"Excel 12.0 Xml;HDR=YES;\"";
using (OleDbConnection conn = new OleDbConnection(connectionstring))
{
conn.Open();
OleDbDataAdapter objDA = new System.Data.OleDb.OleDbDataAdapter
("select * from [" + sheetName + "$]", conn);
DataSet excelDataSet = new DataSet();
objDA.Fill(excelDataSet);
return excelDataSet;
}
The popup has an option to Debug or continue. When i debug i get "An unhandled win32 exception occurred in w3wp.exe"
Has anyone faced this issue?
I know this question has been asked plenty but I can't find a fix. I am trying to import an excel file and bind it to an aspxgridview. I have installed the AccessDatabaseEngine_X64.exe redistributable on my local machine, but I keep geeting the "The Microsoft.ACE.OLEDB.12.0 provider is not registered on the local machine". below is a snippet of code.
string connStr = "";
string ext = Path.GetExtension(fuImportPP.FileName).ToLower();
string path = Server.MapPath("~/ExcelToGrid/" + fuImportPP.FileName);
gv = new GridView();
fuImportPP.SaveAs(path);
//if (ext.Trim() == ".xls")
// //connection string for that file which extantion is .xls
// connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
//else if (ext.Trim() == ".xlsx")
connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
string query = "SELECT * FROM [GridViewExport$]";
OleDbConnection conn = new OleDbConnection(connStr);
if (conn.State == ConnectionState.Closed)
conn.Open();
OleDbCommand cmd = new OleDbCommand(query, conn);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
gv.DataSource = ds.Tables[0];
gv.DataBind();
phGridViewHolder.Controls.Add(gv);
conn.Close();
First check the bitness of your office installation
Since you're using c#, Try and go to Project > Project Properties > Click on the Build Tab > Where it Platform Target > Change it so that for example if you Office Installation is 32 Bit then change it to 32 Bit and if it is 64 Bit then change it to 64 Bit.
After that save and try running your project again.
please need your help .
in my application i am using the file upload control to import excel file into database. when i am running the application in localhost it is working fine, but after deploying the application when i am running the application from same or other machine, the file upload control is not working and get the below error.
The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.
Uploading code :
if (FileUpload.HasFile)
{
string path = string.Concat((Server.MapPath("~/Temp/" + FileUpload.FileName)));
FileUpload.PostedFile.SaveAs(path);
OleDbConnection OleDbCon = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties=Excel 12.0;");
OleDbCommand cmd = new OleDbCommand("Select * from [Sheet1$]", OleDbCon);
OleDbDataAdapter objAdapter = new OleDbDataAdapter(cmd);
OleDbCon.Open();
DbDataReader dr = cmd.ExecuteReader();
string con_str = ConfigurationManager.ConnectionStrings[1].ConnectionString;
SqlBulkCopy bulkInsert = new SqlBulkCopy(con_str);
bulkInsert.DestinationTableName = "[dbo].[HP_temp]";
bulkInsert.WriteToServer(dr);
OleDbCon.Close();
Array.ForEach(Directory.GetFiles((Server.MapPath("~/Temp/"))), File.Delete);
Label_UploadMsg.ForeColor = Color.Green;
Label_UploadMsg.Text = "Imported sucess";
}
else
{
Label_UploadMsg.ForeColor = Color.Red;
Label_UploadMsg.Text = "Error";
}
Install the Microsoft Access Database Engine 2010 Redistributable.
https://www.microsoft.com/en-gb/download/details.aspx?id=13255
Pay particular attention to the type of version (32/64 bit).
Reference: http://www.mikesdotnetting.com/article/280/solved-the-microsoft-ace-oledb-12-0-provider-is-not-registered-on-the-local-mac
How can I get the data in a .dbf file using c#??
What I want to do is to read the data in each row (same column) to further process them.
Thanks.
You may create a connection string to dbf file, then using OleDb, you can populate a dataset, something like:
string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=directoryPath;Extended Properties=dBASE IV;User ID=Admin;Password=;";
using (OleDbConnection con = new OleDbConnection(constr))
{
var sql = "select * from " + fileName;
OleDbCommand cmd = new OleDbCommand(sql, con);
con.Open();
DataSet ds = new DataSet(); ;
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
}
Later you can use the ds.Tables[0] for further processing.
You may also check this article Load a DBF into a DataTable
I found out the accepted answer didn't work for me, as the .dbf files I'm working with are nested in a hierarchy of directories that makes the paths rather long, which, sadly, cause the OleDbCommand object to throw.
I found a neat little library that only needs a file path to work. Here's a little sample adapted from the examples on its GitHub page:
var file = "C:\\Path\\To\\File.dbf";
using (var dbfDataReader = new DbfDataReader(file))
{
while (dbfDataReader.Read())
{
var foo = Convert.ToString(dbfDataReader["FOO"]);
var bar = Convert.ToInt32(dbfDataReader["BAR"]);
}
}
For 64 bit systems I used the Microsoft ACE OLEDB 12.0 data provider, for that provider to work you have to install Microsoft's Access Database Engine 2010
So it looks a lot like the accepted answer but with the provider changed:
string constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\folder;Extended Properties=dBASE IV;User ID=Admin;";
using (OleDbConnection con = new OleDbConnection(constr))
{
var sql = "select * from " + fileName;
OleDbCommand cmd = new OleDbCommand(sql, con);
con.Open();
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
}
I am new in creating application in Visual Studio 2010. I recently created an application which has a MySQL as a database. Now, I am creating an app where I used a MS Access 2007 as a database.
I have this code for the database connection of MySQL:
class DBConn
{
string MyConString = "SERVER=localhost;" + "DATABASE=payroll;" + "UID=root;" + "PASSWORD=admin;";
public DataTable retrieveRecord(string cmd)
{
MySqlConnection con = new MySqlConnection(MyConString);
MySqlCommand command = new MySqlCommand(cmd, con);
MySqlDataAdapter adp = new MySqlDataAdapter(command);
con.Open();
DataSet set = new DataSet();
adp.Fill(set);
con.Close();
return set.Tables[0];
}
}
My problem now is that how to change this code to access the database of MS Access 2007? Please help. Thanks.
Change the connection string to something like this: "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\yourdatabase.mdb;User Id=username;Password=password;"