C# 带有映射列的SQL批量复制

C# 带有映射列的SQL批量复制,c#,linq,sqlbulkcopy,C#,Linq,Sqlbulkcopy,我试图通过映射列名从一个表大容量复制到另一个表,因为源和目标可能并不总是具有相同的列 源可以有8列,目标可以有10列。。我需要映射列并批量复制 尝试了以下代码..无效..获取错误:给定的ColumnName'moduleid'与数据源中的任何列都不匹配 来源:existingtablecolumnsPresent具有[收集时间]、[日志时间]、[模块ID]、[节点]、[原因]、[时间]、[时间戳]、[用例ID] 目标:dataTable。列有[Node]、[Time]、[Reason]、[Mod

我试图通过映射列名从一个表大容量复制到另一个表,因为源和目标可能并不总是具有相同的列

源可以有8列,目标可以有10列。。我需要映射列并批量复制

尝试了以下代码..无效..获取错误:给定的ColumnName'moduleid'与数据源中的任何列都不匹配

来源:existingtablecolumnsPresent具有[收集时间]、[日志时间]、[模块ID]、[节点]、[原因]、[时间]、[时间戳]、[用例ID]

目标:dataTable。列有[Node]、[Time]、[Reason]、[Moduleid]、[Usecaseid]

请告知

 public static void BatchBulkCopy(DataTable dataTable, string DestinationTbl,  List<string> columnMapping,string filename)
    {
        var program = new Program();
        // Get the DataTable 
        DataTable dtInsertRows = dataTable;

        using (SqlBulkCopy sbc = new SqlBulkCopy(program.connectionStr.ToString()))
        {


            try { 
            sbc.DestinationTableName = DestinationTbl.ToLower();

            string sourceTableQuery = "Select top 1 * from " + "[" + dataTable.TableName + "]";
            DataTable dtSource = SqlHelper.ExecuteDataset(program.connectionStr.ToString(), CommandType.Text, sourceTableQuery).Tables[0];

            for (int i = 0; i < dataTable.Columns.Count; i++)
            {    //check if destination Column Exists in Source table
                if (dtSource.Columns.Cast<DataColumn>().Select(a => "[" + a.ColumnName.ToLower() + "]").Contains(dataTable.Columns[i].ToString().ToLower()))//contain method is not case sensitive
                {
                        List<string> existingtablecolumnsPresent = dtSource.Columns.Cast<DataColumn>().Select(a => "[" + a.ColumnName.ToLower() + "]").Distinct().OrderBy(t => t).ToList();

                        int sourceColumnIndex = existingtablecolumnsPresent.IndexOf(dataTable.Columns[i].ToString().ToLower());//Once column matched get its index
                    sbc.ColumnMappings.Add(dtSource.Columns[sourceColumnIndex].ToString(), dtSource.Columns[sourceColumnIndex].ToString());//give coluns name of source table rather then destination table so that it would avoid case sensitivity
                }

            }
            sbc.WriteToServer(dtInsertRows);
            sbc.Close();
        }

        catch (Exception ex)
        {
            Log.WriteLog("BatchBulkCopy" + " - " + filename, dataTable.TableName, ex.Message.ToString());

            // To move a file or folder to a new location:
            //if (File.Exists(program.sourceFile + filename))
            //    System.IO.File.Move(program.sourceFile + filename, program.failedfiles + filename);

        }

根据要求,创建一个数据表,其中包含要插入的列—不包括其他列。确保您遗漏的任何列在表中标记为NULL或具有默认值约束,除非您向我显示您的表,否则我无法向您演示如何执行该操作

 //This first method is psuedoCode to explain how to create your datatable. You need to do it in the way that makes sense for you.
 public DataTable createDataTable(){
    List<string> excludedColumns = new List<string>();
    excludedColumns.Add("FieldToExclude");
    //...
    DataTable dt = new DataTable();
    foreach(string col in getColumns(myTable)){
         if(!excludedColumns.Contains(name)){
         DataColumn dC = new DataColumn(name,type);
         DataTable.Add(dC);
     }
     return dt;
}


 public List<string> getColumns(string tableName)
    {
        List<string> ret = new List<string>();
        using (SqlConnection conn = getConn())
        {
            conn.Open();
            using (SqlCommand com = conn.CreateCommand())
            {
                com.CommandText = "select column_Name from information_schema.COLUMNS where table_name = @tab";
                com.Parameters.AddWithValue("@tab", tableName);
                SqlDataReader read = com.ExecuteReader();
                While(read.Read()){
                ret.Add(Convert.ToString(read[0]);
            }
            conn.Close();
        }
        return ret;
    }
 //Now, you have a DataTable that has all the columns you want to insert. Map them yourself in code by adding to the appropriate column in your datatable.
 public bool isCopyInProgess = false;//not necessary - just part of my code
    public  void saveDataTable(string tableName, DataTable table)
    {
        using (SqlConnection conn = getConn())
        {
            conn.Open();
            using (var bulkCopy = new SqlBulkCopy(conn))//, SqlBulkCopyOptions.KeepIdentity))//un-comment if you want to use your own identity column
            {
                // my DataTable column names match my SQL Column names, so I simply made this loop. However if your column names don't match, just pass in which datatable name matches the SQL column name in Column Mappings
                foreach (DataColumn col in table.Columns)
                {
                    //Console.WriteLine("mapping " + col.ColumnName+" ("+does_Column_Exist(col.ColumnName,"Item").ToString()+")");
                    bulkCopy.ColumnMappings.Add(col.ColumnName, "["+col.ColumnName+"]");
                   // Console.WriteLine("ok\n");
                }
                bulkCopy.BulkCopyTimeout = 8000;
                bulkCopy.DestinationTableName = tableName;
                bulkCopy.BatchSize = 10000;
                bulkCopy.EnableStreaming = true;
                //bulkCopy.SqlRowsCopied += BulkCopy_SqlRowsCopied;
                //bulkCopy.NotifyAfter = 10000;
                isCopyInProgess = true;
               bulkCopy.WriteToServer(table);
            }
            conn.Close();
        }
    }

有没有具体的原因让你需要C?似乎阻力最小的方法是使用SQL来完成这项工作

INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;

尝试了以下代码..不起作用…-它在哪里失败了?它是怎么失败的?生成了哪些错误消息?为批量事务创建数据表-使列与数据库相同。仅映射数据表列。Malke确保其他列允许空值或默认值构造函数。在问题中添加了错误详细信息@香农:你能提供任何样本吗?在你的select语句中,你可以使用“colA AS colZ”来转换名称。我的coulmn名称不匹配。。我在我的代码中处理过这件事。。但是我仍然得到了问题中提到的错误,提供了源表和目标表的详细信息..好的-我们使用上面的方法检查列是否存在。。。将有助于您的交叉映射我想我对[…数据库没有大括号表示表的列名,但datatable的每一列都有[node]。.如何删除datatable中每一列的节点..我尝试过这个..sbc.ColumnMappings.Addcol.ColumnName.ToLower.Replace[,string.Empty.Replace],string.Empty,col.ColumnName.ToLower.Replace[,string.Empty.Replace],string.Empty;但是仍然获取给定的ColumnName“node”与数据源中的任何列都不匹配。错误..@Shannon可以提供建议吗
INSERT INTO table2
(column_name(s))
SELECT column_name(s)
FROM table1;