尝试加载 Excel 信息时出错



我有C# WinForms应用程序,可以从Excel工作表中获取信息并将其存储到我的数据库中,我的代码在我的PC上运行良好,但是当我更改它时出错

System.InvalidOperationException:"'Microsoft.ACE.OLEDB.12.0' 提供程序未在本地计算机上注册。

显示。我通过将项目平台从 x86 更改为 x64 解决了这个问题,但是当我使用 x64 启动项目时,我无法为很多文件充电,所以我必须在 x86 上运行它。伙计们有什么解决方案吗?

Rq:我的代码:

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();
}

您需要从此处下载 32 位驱动程序 微软下载(我认为这是正确的(,但您可能会遇到的困难是机器不能同时安装 64 位和 32 位版本。

使用 EPPlus 是一种更现代的方法。

相关内容

最新更新