SqlBulkCopy - 将行连同附加值添加到数据库

SqlBulkCopy - Add rows along with additional values to database

我正在尝试使用 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 服务器数据库 table。但我的要求是我需要添加 excel sheet 的值加上我自己的值 autogenerated studentid.

所以我的问题是如何添加新值(比如学生 ID、批号等)以及从 excel 读取的值。并将这些值添加到 excel 数据的每一行。

示例:-

excel 包含以下列

CustomerID,City,Country,PostalCode

现在我需要通过添加一些新列来向 sql 服务器添加值

StudentID,CustomerID,BatchCode,City,Country,Email,PostalCode

我该怎么做

请帮忙

您可以执行以下操作:

  • 加载 excel 数据到数据 table,
  • 将剩余的列添加到数据 table、
  • 设置新的列值
  • SqlBulk将数据table复制到SQL服务器。

像这样:

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);