using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
 
using System.Data.SQLite;
using System.Data.OleDb;
using System.IO;
 
using MySql.Data.MySqlClient;
 
using Dapper;
using  *******.DataDefine;  //数据库引用
 
namespace LangLibSpeakingImport
{
    public partial class Import : Form
    {
        public Import()
        {
            InitializeComponent();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            string excelName = string.Empty;
            string dirName = Path .Combine(Directory.GetCurrentDirectory(), "root" );//bin目录下的文件夹的文件名
            string[] subDirs = Directory.GetDirectories(dirName);
 
            List<SK_SysTopicInfo > topicList = new List <SK_SysTopicInfo>();
 
            foreach (string dNames in subDirs)
            {
                string dirSeName = Path .Combine(Directory.GetCurrentDirectory(), dNames);
                string[] subSeDirs = Directory.GetDirectories(dirSeName);                                //获取文件夹下子文件夹下的子Excel的路径
                foreach (string secName in subSeDirs)
                {
                    try
                    {
                        excelName = secName.Substring(secName.LastIndexOf(@"\" ) + 1, 4);
                        string fileName = Path .Combine(secName, excelName + ".xlsx" );      //获取文件夹下子文件夹下的子Excel的路径
 
                        var connectionString = string .Format("Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}; Extended Properties=Excel 12.0;", fileName);  //连接Excel表的数据
 
                        var adapter1 = new OleDbDataAdapter( "SELECT * FROM [SK_SysTopic$]", connectionString);   //Excel表的share名
                      
                        var ds = new DataSet();
 
                        adapter1.Fill(ds, "SysTopicDatas" );
 
                        DataTable readingTopicTable = ds.Tables["SysTopicDatas" ];
                        foreach (DataRow row in readingTopicTable.Rows)
                        {
                            SK_SysTopicInfo topicQuest = new SK_SysTopicInfo ();
                            topicQuest.Id = Convert.ToString(row["Id"]);//字段名
                            topicQuest.TitleText = Convert .ToString(row["TitleText"]);//字段名
                            topicQuest.MediaLength = Convert .ToString(row["MediaLength"]);//字段名
                            topicQuest.Difficulty = Convert .ToString(row["Difficulty"]);//字段名
                            topicQuest.Source = Convert.ToString(row["Source"]);//字段名
                            topicQuest.Scene = Convert.ToString(row["Scene"]);//字段名
                            topicList.Add(topicQuest);
                        }
                        using (MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection ("Server=*******;Database=***;Uid=**;Pwd=****;Charset=utf8;" ))//连接数据库
                        {
                            try
                            {
                                connection.Open();
                                connection.Execute( "delete from SK_SysTopic" );
                                connection.Execute( "Insert into SK_SysTopic(Id, TitleText, MediaLength, Difficulty, Source, Scene) values(@Id, @TitleText, @MediaLength, @Difficulty, @Source, @Scene)", topicList);//添加数据
                            }
                            catch (Exception )
                            {
                                throw;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        this.textBox1.Text += string.Format( "{0} Exception caught.", ex);
                    }
                }
            }
        }
    }
}

WINFrom Excal 数据导入数据库的更多相关文章

  1. [Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!

    引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...

  2. 将Excel中数据导入数据库(三)

    上篇文章将Excel中数据导入数据库时,将从Excel读入的数据均转换成了数据库相应字段的类型,其实这是没有必要的,因为对于数据库各种类型的插入,均可以字符串格式插入.比如表WQ_SWMSAR_A字段 ...

  3. 将Excel中数据导入数据库(二)

    在上篇文章中介绍到将Excel中数据导入到数据库中,但上篇文章例子只出现了nvachar类型,且数据量很小.今天碰到将Excel中数据导入数据库中的Excel有6419行,其中每行均有48个字段,有i ...

  4. 将Excel中数据导入数据库(一)

    在工作中经常要将Excel中数据导入数据库,这里介绍一种方法. 假如Excel中的数据如下: 数据库建表如下: 其中Id为自增字段: Excel中数据导入数据库帮助类如下: using System; ...

  5. 使用python将excel数据导入数据库

    使用python将excel数据导入数据库 因为需要对数据处理,将excel数据导入到数据库,记录一下过程. 使用到的库:xlrd 和 pymysql (如果需要写到excel可以使用xlwt) 直接 ...

  6. 使用openpyxl模块将Excel中的数据导入数据库

    这里将不介绍openpyxl模块的详细操作. 主要就是记录一个使用openpyxl模块将Excel表格的数据导入数据库中的实例. from openpyxl import load_workbook ...

  7. 读取Execl表数据 导入数据库

    不知不觉博客园园林都两年多了,我是今年毕业的应届生,最近公司项目需要改动,很多的数据需要导入,很多的实体类需要些.考虑到这些问题自己写了两个winform版的小工具,一个是读取Execl数据导入数据库 ...

  8. 转:[Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!

    引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...

  9. 将Excel数据导入数据库

    Excel如下,这页工作表名叫“线路” 数据库表如下 using System; using System.Collections.Generic; using System.Linq; using ...

随机推荐

  1. Entity Framework 6, database-first with Oracle

    Entity Framework 6, database-first with Oracle 转载自http://csharp.today/entity-framework-6-database-fi ...

  2. 解决jquery1.9不支持browser对象的问题||TypeError: $.browser is undefined

    在插件的源代码里插入如下代码: (function(jQuery){ if(jQuery.browser) return; jQuery.browser = {}; jQuery.browser.mo ...

  3. Java File创建新目录和文件

    创建目录 当不存在目录aa文件夹时: File file1=new File("/aa"); Boolean aa=file.mkdir();// true File file1= ...

  4. 如何更改OS系统下截图生成图片格式 jpg pdf

    Mac系统下快速截屏的快捷键: 1.截全屏: shift + command + 3 2.选取截屏 shift + command + 4 生成的图片,系统默认格式忘了,反正不好用,用下面命令更改生成 ...

  5. 15.Xcode8 升级遇到的问题

    一:注释快捷键cmd+/不能用,解决方法: 1. Swift_3.0 没法快捷键(command+/)注释的原因:这个是因为苹果解决xcode ghost,把插件屏蔽了. 2. 解决办法: (1) 终 ...

  6. python 跨模块实现按照文件大小,日期实现日志分割,反转

    笔者的一个自动化测试平台项目,采用了python作为后端服务器语言.项目基于快速成型目的,写了一个极其简陋的日志记录功能,支持日志记录到文件和支持根据日志级别在终端打印不同颜色的log.但随着测试平台 ...

  7. 【洛谷P3143】Diamond Collector

    算是一道dp 首先,排序好每一个架子上都是一段区间,然后只需要统计每个点向左向右最长延伸的区间. 所以我们预处理出每个点以左.以右最大能延伸的长度(最多能选几个差值不超过k的) 然后枚举每个点作为断点 ...

  8. python中文编码

    前面章节中我们已经学会了如何用 Python 输出 "Hello, World!",英文没有问题,但是如果你输出中文字符"你好,世界"就有可能会碰到中文编码问题 ...

  9. 图层的核心动画(CABaseAnimation)续

    Main.storyboard ViewController.m // //  ViewController.m //  8A01.核心动画 // //  Created by huan on 16/ ...

  10. BOOST1.54简化编译

    1.设置环境变量 BOOST_ROOT E:\Projects\00.CODE.SDK\boost_1_54_0 2.运行bootstrap.bat 3.运行: bjam install --tool ...