码迷,mamicode.com
首页 > 数据库 > 详细

ASP.NET-【Excel】-将Excel中的数据批量加载到SQLserver数据库

时间:2015-01-31 11:59:13      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:

用到了一个SqlBulkCopy的类

核心代码分析

代码我还没有测试过

                    string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path);
                    // Create Connection to Excel Workbook
                    using (OleDbConnection connection =
                                 new OleDbConnection(excelConnectionString))
                    {
                        OleDbCommand command = new OleDbCommand
                                ("Select * FROM [Sheet1$]", connection);
                        connection.Open();
                        // Create DbDataReader to Data Worksheet
                        using (DbDataReader dr = command.ExecuteReader())
                        {
                            // SQL Server Connection String
                            string sqlConnectionString = @"Data Source=.\sqlexpress;Initial Catalog=ExcelDB;Integrated Security=True";
                            // Bulk Copy to SQL Server
                            using (SqlBulkCopy bulkCopy =
                                       new SqlBulkCopy(sqlConnectionString))
                            {
                                bulkCopy.DestinationTableName = "Employee";
                                bulkCopy.WriteToServer(dr);
                                Label1.Text = "The data has been exported succefuly from Excel to SQL";
                            }
                        }
                    }

 

ASP.NET-【Excel】-将Excel中的数据批量加载到SQLserver数据库

标签:

原文地址:http://www.cnblogs.com/Sky-cloudless/p/4263782.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!