读取Excel的记录并导入SQL数据库

2018-06-22 07:53:44来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

准备一下,近段时间,需要把Excel的数据导入数据库中。

引用命名空间:

using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;

 

你可以写一个方法,是去读取Excel文档的方法,返回DataSet数据集:

 

private DataSet ImportExcelToDataSet(string virtualPath, string sqlQueryStatement)
    {
        string excelConnectionString = DB.ExcelConnectionString(Server.MapPath(virtualPath));
        OleDbConnection dc = new OleDbConnection(excelConnectionString);
        OleDbDataAdapter da = new OleDbDataAdapter(sqlQueryStatement, dc);
        DataSet ds = new DataSet();
        da.Fill(ds);
        return ds;
    }
Source Code


导入数据库,在数据库中,需要创建一个表来存储Excel导入来的数据:



接下来,你可以使作SqlBulkCopy的方法进行复制数据库:

 

try
        {
            string cs = ConfigurationManager.ConnectionStrings["InsusSqlConnectionString"].ConnectionString;
            using (SqlConnection sqlConn = new SqlConnection(cs))
            {
                string sqlQueryStatement = "SELECT [Material],[Plnt],[Level],[Item],[Component],[Object description] FROM [Sheet1$]";
                string virtualPath = "~/App_Data/Book1.xlsx";

                DataSet ds = ImportExcelToDataSet(virtualPath, sqlQueryStatement);

                DataTable dt = ds.Tables[0];

                sqlConn.Open();
                using (SqlBulkCopy sqlbc = new SqlBulkCopy(sqlConn))
                {
                    sqlbc.DestinationTableName = "BOM";
                    sqlbc.ColumnMappings.Add("Material", "Material");
                    sqlbc.ColumnMappings.Add("Plnt", "Plnt");
                    sqlbc.ColumnMappings.Add("Level", "Level");
                    sqlbc.ColumnMappings.Add("Item", "Item");
                    sqlbc.ColumnMappings.Add("Component", "Component");
                    sqlbc.ColumnMappings.Add("Object description", "Object description");
                    sqlbc.WriteToServer(dt);
                    Response.Write("数据导入成功!");
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
Source Code


演示:

 

 

下面内容于2017-10-17 14:38分添加:

使用sqlDataReader来导入,没有做过多的测试,速度与DataSet快不了多少:


private OleDbDataReader ImportExcelToDataReader(string virtualPath, string sqlQueryStatement)
    {
        string excelConnectionString = DB.ExcelConnectionString(Server.MapPath(virtualPath));
        OleDbConnection oleConn = new OleDbConnection(excelConnectionString);
        OleDbCommand oleComm = new OleDbCommand(sqlQueryStatement, oleConn);

        oleConn.Open();
        return oleComm.ExecuteReader();
    }
Source Code


SqlBulkCopy的方法:


 

 string sqlQueryStatement = "SELECT [Material],[Plnt],[Level],[Item],[Component],[Object description] FROM [Sheet1$]";
        string virtualPath = "~/App_Data/Book1.xlsx";

        string cs = ConfigurationManager.ConnectionStrings["InsusSqlConnectionString"].ConnectionString;
        SqlConnection sqlConn = new SqlConnection(cs);

        OleDbDataReader reader = ImportExcelToDataReader(virtualPath, sqlQueryStatement);

        using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConn))
        {
            bulkCopy.DestinationTableName = "BOM";
            try
            {
                sqlConn.Open();
                bulkCopy.WriteToServer(reader);
                Response.Write("数据导入成功。");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                reader.Close();
                sqlConn.Close();
            }
        }
Source Code

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Asp.Net Web API(一)

下一篇:ASP.NET 使用My97DatePicker日期控件