Excel文件到C#中的数据库 [英] Excel file to database in C#

查看:59
本文介绍了Excel文件到C#中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已在桌面上的一个文件夹中创建了一个保存Excel文件的文件夹.在运行时的编程中,当我单击上载一个文件夹时,excel文件应自动将excel文件数据保存到数据库中.

问候
Balamurugan

Hi,
I have created a folder in my desktop inside the folder i have saved the Excel file.In my programming during run time when i clicked the upload a folder the excel file should automaticclay saved the excel file datas into database.

Regards
Balamurugan

推荐答案

更新按钮单击
1.首先,您需要将数据从Excel工作表读取到DataTable(使用Excel的Oledb Provider)

Excel的连接字符串
Update Button Click
1. first you need to read data from Excel sheet to DataTable(Using Oledb Provider for Excel)

Connection String for Excel
string Con_Str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + DB_Path + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";  


2.如果要在数据库和Excel工作表之间进行比较,则

这里Fdt表示ExcelDataTable,而Adt表示SqlDataTable


2. if you want Comparison between u r Database and Excel Sheet Then

Here Fdt means ExcelDataTable and Adt Means u r SqlDataTable

public DataTable CompareDataTable(DataTable Fdt, DataTable Adt)
      {
          DataTable Cdt = new DataTable(); // Comparison DataTable
          Cdt = Fdt.Clone();
          bool flag = false;
          try
          {
              foreach (DataRow Mrow in Fdt.Rows)
              {
                  var Marray = Mrow.ItemArray;
                  foreach (DataRow Arow in Adt.Rows)
                  {
                      var Aarray = Arow.ItemArray;
                      if (Aarray.SequenceEqual(Marray))
                      {
                          flag = true;
                          break;
                      }
                      else
                      {
                          flag = false;
                      }
                  }
                  if (flag == false)
                  {
                      Cdt.LoadDataRow(Mrow.ItemArray, LoadOption.PreserveChanges);
                  }
              }
          }
          catch (Exception ex)
          {

          }
          return Cdt;
      }


现在您获得了比较表,然后使用foreach循环可以在运行时将Cdt插入Sql

希望对您有帮助

如果有任何查询,请留言

[edit]添加了代码块[/edit]


Now u got Comparison Table Then using foreach loop you can Insert Cdt into Sql at run time

I hope this will help you

If any Query pls Comment

[edit]code block added[/edit]


这篇关于Excel文件到C#中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆