SqlBulkCopy - 将行及其他值添加到数据库

时间:2015-11-01 08:12:38

标签: c# asp.net sql-server ado.net sqlbulkcopy

我正在尝试使用SqlBulkCopy将excel数据添加到sql数据库的代码片段。代码段如下所示



OleDbConnection connection=null;
        string FilePath="";
         try
            {
                if (FileUpload1.HasFile)
                {
                    FileUpload1.SaveAs(Server.MapPath("~/UploadedFolder/"+FileUpload1.FileName));
                    FilePath = Server.MapPath("~/UploadedFolder/"+FileUpload1.FileName);
         
                }

                string path = FilePath;
                // Connection String to Excel Workbook
                string excelConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 8.0", path);
                connection = new OleDbConnection();
                connection.ConnectionString = excelConnectionString;
                OleDbCommand command = new OleDbCommand("select * from [Sheet1$]", connection);
                connection.Open();
                // Create DbDataReader to Data Worksheet
                DbDataReader dr = command.ExecuteReader();

                // SQL Server Connection String
                string sqlConnectionString = @"Data Source=sample;Initial Catalog=ExcelImport;User ID=sample;Password=sample";

                // Bulk Copy to SQL Server 
                SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnectionString);
                bulkInsert.DestinationTableName = "Customer_Table";
                bulkInsert.WriteToServer(dr);
           
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            finally
            {
                connection.Close();
                Array.ForEach(Directory.GetFiles(Server.MapPath("~/UploadedFolder/")), File.Delete);
            }




这将excel文件中的数据添加到sql server数据库表中。但我的要求是我需要添加excel表的值,另外我自己的值表示autogenerated studentid。

所以我的问题是如何添加新值(比如studentid,batchcode等)以及从excel读取的值。并将这些值添加到excel数据的每一行。

实施例: -

excel包含以下列

CustomerID,City,Country,PostalCode

现在我需要通过添加一些新列作为

来为sql server添加值

StudentID,CustomerID,BatchCode,城市,国家,电子邮件,PostalCod e

我该怎么做

请帮忙

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

  • 将Excel数据加载到数据表中,
  • 将剩余的列添加到数据表
  • 设置新列值
  • SqlBulk将数据表复制到SQL Server中。

这样的事情:

DbDataReader dr = command.ExecuteReader();

DataTable table = new DataTable("Customers");
table.Load(dr);
table.Columns.Add("StudentId", typeof(int));
table.Columns.Add("BatchCode", typeof(string));
table.Columns.Add("Email", typeof(string));

foreach (DataRow row in table.Rows)
{
    row["StudentId"] = GetStudentId(row);
    row["BatchCode"] = GetBatchCode(row);
    row["Email"] = GetEmail(row);
}


// SQL Server Connection String
string sqlConnectionString = @"Data Source=sample;Initial Catalog=ExcelImport;User ID=sample;Password=sample";

// Bulk Copy to SQL Server 
SqlBulkCopy bulkInsert = new SqlBulkCopy(sqlConnectionString);
bulkInsert.DestinationTableName = "Customer_Table";
bulkInsert.WriteToServer(table);