大佬教程收集整理的这篇文章主要介绍了如何将 Excel 文件数据以 c# windows 形式导入 SQLite 数据库?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将 excel 文件数据导入 SQLite 数据库。我使用了 2 个按钮,导入和保存按钮。
这是导入按钮的代码,其中我尝试从给定路径获取文件并将文件数据加载到 datagrIDvIEw。
private voID imporT_Btn_Click(object sender,EventArgs E)
{
String filename= @"C:\Users\***\documents\Customers.xLSX";
Workbook workbook = new Workbook();
workbook.LoadFromfile(fileName);
Worksheet sheet = workbook.Worksheets[0];
this.dataGrIDVIEw1.Datasource = sheet.ExportDatatable();
}
这里我将数据导入到 SQLite 数据库表中。
private voID save_btn_Click(object sender,EventArgs E)
{
String code = "C-0002";
sqliteConnection Con = new sqliteConnection("Data source=|DataDirectory|JDS_DB.sqlite");
sqliteCommand com;
String str;
Con.open();
for (int index = 0; index < dataGrIDVIEw1.Rows.Count - 1; index++)
{
str = "insert into Customers(name,Code,Group_,Address,Phone,Cell,Email_Address,Date,opening_balancE)values(@name,@Code,@Group_,@Address,@phone,@Cell,@Email_Address,@Date,@opening_balancE)";
com = new sqliteCommand(str,Con);
com.Parameters.AdDWithValue("@name",dataGrIDVIEw1.Rows[index].Cells[0].Value.ToString());
com.Parameters.AdDWithValue("@Code",codE);
com.Parameters.AdDWithValue("@Group_",dataGrIDVIEw1.Rows[index].Cells[1].Value.ToString());
com.Parameters.AdDWithValue("@Address",dataGrIDVIEw1.Rows[index].Cells[2].Value.ToString());
com.Parameters.AdDWithValue("@phone",dataGrIDVIEw1.Rows[index].Cells[3].Value.ToString());
com.Parameters.AdDWithValue("@Cell",dataGrIDVIEw1.Rows[index].Cells[4].Value.ToString());
com.Parameters.AdDWithValue("@Email_Address",dataGrIDVIEw1.Rows[index].Cells[5].Value.ToString());
com.Parameters.AdDWithValue("@Date",dataGrIDVIEw1.Rows[index].Cells[6].Value.ToString());
com.Parameters.AdDWithValue("@opening_balance",dataGrIDVIEw1.Rows[index].Cells[7].Value.ToString());
com.ExecuteNonquery();
messageBox.Show("added");
}
Con.Close();
}
问题:
它工作正常,但我想从excel中读取数据并直接将其导入数据库表中,而不是使用datagrIDvIEw。如果有人知道怎么做,请告诉我,谢谢
您可以使用 OleDbConnection 打开到 Excel 文件的直接连接,在该连接上打开一个 OleDbDataReader,然后逐行读取到 sqlite 连接中。
,未测试,因为您没有告诉我们您使用的是哪种 Excel 集成(互操作,...)。 基本上你将结果存储在一个变量或属性中,这里我使用了“数据”,稍后直接访问它。
private void save_btn_Click(object sender,EventArgs E)
{
// Load the data
String filename = @"C:\Users\***\Documents\Customers.xLSX";
Workbook workbook = new Workbook();
workbook.LoadFromFile(fileName);
Worksheet sheet = workbook.Worksheets[0];
DataTable data = sheet.ExportDataTable();
// Write the data
String code = "C-0002";
sqliteConnection Con = new sqliteConnection("Data source=|DataDirectory|JDS_DB.sqlite");
sqliteCommand com;
String str;
Con.open();
foreach (DataRow row in data.Rows) //iterate over all rows
{
str = "insert into Customers(Name,Code,Group_,Address,Phone,Cell,Email_Address,Date,Opening_balancE)values(@Name,@Code,@Group_,@Address,@phone,@Cell,@Email_Address,@Date,@Opening_balancE)";
com = new sqliteCommand(str,Con);
com.Parameters.AddWithValue("@Name",row.Field<String>(0));
com.Parameters.AddWithValue("@Code",row.Field<String>(1));
//... you get the point :)
com.ExecuteNonQuery();
messageBox.Show("added");
}
Con.Close();
}
以上是大佬教程为你收集整理的如何将 Excel 文件数据以 c# windows 形式导入 SQLite 数据库?全部内容,希望文章能够帮你解决如何将 Excel 文件数据以 c# windows 形式导入 SQLite 数据库?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。