VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > c#教程 >
  • C#教程之C#教程之c# excel如何导入到sqlserver数据库

本站最新发布   C#从入门到精通
试听地址  
https://www.xin3721.com/eschool/CSharpxin3721/

最近在做这个如何把excel导入到数据库中,经过多方查找,终于找到一个适合的,并且经过自己的完善可以正常使用(忘记原作者博客的链接地址了,敬请见谅)

  1. 首先是窗体的创建,文本框显示文件的路径,按钮执行操作,DataGridView显示导入的信息
  2. 代码如下:可根据自己的需求进行修改,我是要导入之后就对我的另一窗体进行刷新,定义了委托,你们可以忽略。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    //定义委托
    public delegate void Refresh();
     
    //定义事件
    public event Refresh myRefresh;
    public ExcelRoprtForm()
    {
        InitializeComponent();
    }
     
    private void button1_Click(object sender, EventArgs e)
    {
        //选中导入的文件
        try
        {
            //openFileDialog1.Filter = "Excel 文件|*.xls";//指定存放文件格式类型
            OpenFileDialog fd = new OpenFileDialog();
            fd.Filter = "Excel文件(*.xls,xlsx)|*.xls;*.xlsx";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                string fileName = fd.FileName.ToString();
                this.textBox1.Text = fileName;
            }
     
        }
        catch (Exception ee)
        {
            MessageBox.Show("打开文件出错!" + ee.Message.ToString());
        }
    }
     
    private DataSet xsldata(string filepath)
    {
        string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;IMEX=1'";
     
        //string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + filepath + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'";
     
        System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(strCon);
     
        string strCom = "SELECT * FROM [Sheet1$]";
     
        Conn.Open();
     
        System.Data.OleDb.OleDbDataAdapter myCommand = new System.Data.OleDb.OleDbDataAdapter(strCom, Conn);
     
        DataSet ds = new DataSet();
     
        myCommand.Fill(ds, "[Sheet1$]");
        dataGridView1.DataSource = ds.Tables[0];
        Conn.Close();
        return ds;
    }
    private void button2_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "")
        {
            MessageBox.Show("请选择要导入的Excel文档!""系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
        }
        string filepath = textBox1.Text;
        string strcon1 = ConfigurationManager.ConnectionStrings["connString"].ToString();
        SqlConnection conn = new SqlConnection(strcon1);//链接数据库
        conn.Open();
        try
        {
            DataSet ds = new DataSet();
            //取得数据集
            //调用上面的函数
            ds = xsldata(filepath);
            int errorcount = 0;//记录错误信息条数
     
            int insertcount = 0;//记录插入成功条数
     
            int updatecount = 0;//记录更新信息条数
     
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                string carnumber = ds.Tables[0].Rows[i][0].ToString();
                int carstatus = Convert.ToInt32(ds.Tables[0].Rows[i][1].ToString());
                int cartype = Convert.ToInt32(ds.Tables[0].Rows[i][2].ToString());
                string carbrand = ds.Tables[0].Rows[i][3].ToString();
     
                if (carnumber != "" && carstatus != 0 && cartype != 0)
                {
                    SqlCommand selectcmd = new SqlCommand("select count(*) from CarInfo where CarNumber='" + carnumber + "'", conn);
                    
                    int count = Convert.ToInt32(selectcmd.ExecuteScalar());
                    if (count > 0)
                    {
                        updatecount++;
                    }
                    else
                    {
                        SqlCommand insertcmd = new SqlCommand("insert into CarInfo(CarNumber,CarStatusID,CarTypeID,CarBrand) values(" "'" + carnumber + "'," + carstatus + "," + cartype + ",'" + carbrand + "'" ")", conn);
     
                        insertcmd.ExecuteNonQuery();
     
                        insertcount++;
     
                    }
                }
                else
                {
                    //MessageBox.Show("电子表格信息有错!");
                    errorcount++;
                }
            }
            myRefresh();
            MessageBox.Show(insertcount + "条数据导入成功!" + updatecount + "条数据重复!" + errorcount + "条数据部分信息为空没有导入!");
        }
        catch (Exception ex)
        {
     
            MessageBox.Show(ex.Message);
        }
     
        finally
        {
            conn.Close();
     
        }
    }
  3. 如何使用OS模块中的stat方法
  4. Python os 模块
  5. seek() 方法
  6. python打开文件实例1
  7. Python写入文件
  8. 什么是流?
  9. 文件操作如何进制逐行读取
  10. Python相对路径
  11. with创建临时运行环境
  12. Python文件操作
  13. .Net Standard(.Net Core)实现获取配置信息
  14. Linux PXE + Kickstart 自动装机
  15. Shell 编程 基础
  16. Shell 编程 条件语句
  17. CentOS8-网卡配置及详解
  18. Linux中LVM逻辑卷管理
  19. 1.数码相框-相框框架分析(1)
  20. Ubuntu armhf 版本国内源
  21. Linux中raid磁盘阵列
  22. 搭建简易网站
  23. access教程之Access简介
  24. mysql 安装了最新版本8.x版本后的报错:
  25. Mysql空间数据&空间索引(spatial)
  26. 如何远程连接SQL Server数据库的图文教程
  27. 复制SqlServer数据库的方法
  28. 搜索sql语句
  29. sql中返回参数的值
  30. sql中生成查询的模糊匹配字符串
  31. 数据定义功能
  32. 数据操作功能
相关教程