C# 在使用C发送到SQL Server之前,向excel文件添加额外的列和值#

C# 在使用C发送到SQL Server之前,向excel文件添加额外的列和值#,c#,sql-server,excel,C#,Sql Server,Excel,我正在上传一个Excel文件并将数据插入SQL Server。但问题是,在将Excel数据发送到SQL Server之前,我想在发送之前添加一个额外的列和值 以下是我尝试的代码: using (OleDbConnection exel_con = new OleDbConnection(conString)) { exel_con.Open(); string sheet1 = exel_con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables

我正在上传一个Excel文件并将数据插入SQL Server。但问题是,在将Excel数据发送到SQL Server之前,我想在发送之前添加一个额外的列和值

以下是我尝试的代码:

using (OleDbConnection exel_con = new OleDbConnection(conString))
{
    exel_con.Open();
    string sheet1 = exel_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[12] { new DataColumn("Full Name", typeof(string)),
                    new DataColumn("Email Address", typeof(string)),
                    new DataColumn("ID Type",typeof(string)),
                    new DataColumn("ID Number", typeof(string)),
                    new DataColumn("Gender",typeof(string)),
                    new DataColumn("Date of Birth", typeof(DateTime)),
                    new DataColumn("Nationality",typeof(string)),
                    new DataColumn("Married Status", typeof(string)),
                    new DataColumn("Join Date", typeof(DateTime)),
                    new DataColumn("Position",typeof(string)),
                    new DataColumn("Salary/Wages Frequency", typeof(string)),
                    new DataColumn("Salary/Wages Amount",typeof(int))
    });

    // Where I tried to add new column
    dtExcelData.Columns.Add("AdminId", typeof(int));

    foreach (DataRow row in dtExcelData.Rows)
    {
        // Where I tried to add new value to the column.
        row["AdminId"] = 1;
    }

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

    exel_con.Close();

    string consString = ConfigurationManager.ConnectionStrings["PayrollSystem"].ConnectionString;

    using (SqlConnection con = new SqlConnection(consString))
    {
        using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
        {
            // Set the database table name
            sqlBulkCopy.DestinationTableName = "dbo.Employee";

            //[OPTIONAL]: Map the Excel columns with that of the database table
            sqlBulkCopy.ColumnMappings.Add("AdminId", "AdminId");
            sqlBulkCopy.ColumnMappings.Add("Full Name", "FullName");
            sqlBulkCopy.ColumnMappings.Add("Email Address", "Email");
            sqlBulkCopy.ColumnMappings.Add("ID Type", "IdType");
            sqlBulkCopy.ColumnMappings.Add("ID Number", "IdNumber");
            sqlBulkCopy.ColumnMappings.Add("Gender", "Gender");
            sqlBulkCopy.ColumnMappings.Add("Date of Birth", "DateOfBirth");
            sqlBulkCopy.ColumnMappings.Add("Nationality", "Nationality");
            sqlBulkCopy.ColumnMappings.Add("Married Status", "MaritalStatus");
            sqlBulkCopy.ColumnMappings.Add("Join Date", "JoinDate");
            sqlBulkCopy.ColumnMappings.Add("Position", "Position");
            sqlBulkCopy.ColumnMappings.Add("Salary/Wages Frequency", "SalaryFrequency");
            sqlBulkCopy.ColumnMappings.Add("Salary/Wages Amount", "SalaryAmount");

            con.Open();
            sqlBulkCopy.WriteToServer(dtExcelData);
            con.Close();
        }
    }
}

请提供任何帮助以解决此问题,我们将不胜感激。

您应该在数据表中填充()后将值添加到新列中。在填充之前,它没有任何行可循环

事实上,您应该在填充数据表后添加新列,因为您将使用SELECT*填充它,因此Excel中的列必须与数据表中的列匹配


因此,首先填充数据表。然后添加新列和值。

您尝试的代码有什么问题?如果您有错误,确切的错误消息是什么?@TabAlleman我没有收到任何错误消息,但是我认为我插入到datatable的AdminId将null插入到我的数据库OK,请参阅下面的答案。