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

用到了一个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";

                            }

                        }

                    }

 

你可能感兴趣的:(sqlserver)