简体   繁体   English

如何从Excel / CSV将批量数据导入到SQL Server

[英]How to import bulk data to sql server from excel/csv

我的要求是我在excel / CSV文件中有2l数据,每行都有电子邮件ID,我必须一次性导入这些数据,即一次验证一个数据(电子邮件)将其批量复制到SQL Server。

You could also use Microsoft Visual Studio's Business Intelligence tools. 您还可以使用Microsoft Visual Studio的商业智能工具。 By creating a SSIS (SQL Server Integration Services) project you can use various drag and drop tools to create "packages" (or jobs if you wish) which you can execute to perform jobs like these. 通过创建SSIS(SQL Server集成服务)项目,您可以使用各种拖放工具来创建“程序包”(如果需要,也可以创建作业),然后可以执行这些程序包以执行类似的作业。

You can import data from a wide variety of data sources including Excel, CSV, MySQL, SQL Server and Hadoop to name a few. 您可以从多种数据源(包括Excel,CSV,MySQL,SQL Server和Hadoop)中导入数据。

You can also write that data from those sources to not only SQL Server but a wide variety of other data destinations as well. 您还可以将这些数据从这些源中不仅写入SQL Server中,而且还写入其他各种数据目标中。

I am using Visual Studio 2015 with the Business Intelligence packages installed. 我正在使用已安装商业智能软件包的Visual Studio 2015。

What I would recommend is: 我建议的是:

  1. Start Visual Studio and open a new SSIS (SQL Server Integration Services) project. 启动Visual Studio并打开一个新的SSIS(SQL Server集成服务)项目。
  2. Under the control flow tab. 在控制流选项卡下。 Add a new Data Flow task to the control flow area. 将新的数据流任务添加到控制流区域。
  3. Double click on the control flow item or navigate to the Data Flow tab. 双击控制流项目或导航到“数据流”选项卡。
  4. Make sure you data flow item is selected. 确保选择了数据流项目。 (Should be if you double clicked it.) (如果您双击它,应该是。)
  5. From there you can use the Source and Destination Assistants to transport your data. 在这里,您可以使用“源助手”和“目标助手”来传输数据。
  6. Once done setting up you source, destination, data transformations and checks. 完成设置后,即可进行源,目标,数据转换和检查。 You can hit Start and it will execute the package. 您可以单击开始,它将执行该程序包。

PS: You can also use the script component in the data flow tab to write custom C# script if you want to. PS:您还可以使用数据流选项卡中的脚本组件来编写自定义C#脚本。

If we had an example of the Schema (Table structure) you were transporting from and to it would have helped with providing an example. 如果我们有一个Schema(表结构)的示例,那么您往返之间将提供一个示例。

Best of luck 祝你好运

I have specified the connection strings for the Excel files of both 2003 and 2007 or higher formats in the Web.Config file. 我已经在Web.Config文件中为2003和2007或更高格式的Excel文件指定了连接字符串。

<add name = "Excel03ConString" connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'"/>
<add name = "Excel07+ConString" connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR=YES'"/>

You will need to import the following namespaces. 您将需要导入以下名称空间。

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

Add the following code : 添加以下代码:

//Upload and save the file
    string excelPath = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
    FileUpload1.SaveAs(excelPath);

    string conString = string.Empty;
    string extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
    switch (extension)
    {
        case ".xls": //Excel 97-03
            conString = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
            break;
        case ".xlsx": //Excel 07 or higher
            conString = ConfigurationManager.ConnectionStrings["Excel07+ConString"].ConnectionString;
            break;

    }
    conString = string.Format(conString, excelPath);
    using (OleDbConnection excel_con = new OleDbConnection(conString))
    {
        excel_con.Open();
        string sheet1 = excel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Rows[0]["TABLE_NAME"].ToString();
        DataTable dtExcelData = new DataTable();

        //[OPTIONAL]: It is recommended as otherwise the data will be considered as String by default.
        dtExcelData.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
                new DataColumn("Name", typeof(string)),
                new DataColumn("Salary",typeof(decimal)) });

        using (OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM [" + sheet1 + "]", excel_con))
        {
            oda.Fill(dtExcelData);
        }
        excel_con.Close();

        string consString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(consString))
        {
            using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
            {
                //Set the database table name
                sqlBulkCopy.DestinationTableName = "dbo.tblPersons";

                //[OPTIONAL]: Map the Excel columns with that of the database table
                sqlBulkCopy.ColumnMappings.Add("Id", "PersonId");
                sqlBulkCopy.ColumnMappings.Add("Name", "Name");
                sqlBulkCopy.ColumnMappings.Add("Salary", "Salary");
                con.Open();
                sqlBulkCopy.WriteToServer(dtExcelData);
                con.Close();
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM