SqlBulkCopy Not Working

前端 未结 7 2044
予麋鹿
予麋鹿 2021-02-18 17:54

I have a DataSet populated from Excel Sheet. I wanted to use SQLBulk Copy to Insert Records in Lead_Hdr table where LeadId is PK.

7条回答
  •  执笔经年
    2021-02-18 18:25

    One of the reason is that :SqlBukCOpy is case sensitive . Follow steps:

    1. In that Case first you have to find your column in Source Table by using "Contain" method in C#.
    2. Once your Destination column matched with source column get index of that column and give its column name in SqlBukCOpy .

    For Example:`

    //Get Column from Source table 
      string sourceTableQuery = "Select top 1 * from sourceTable";
       DataTable dtSource=SQLHelper.SqlHelper.ExecuteDataset(transaction, CommandType.Text, sourceTableQuery).Tables[0];// i use sql helper for executing query you can use corde sw
    
     for (int i = 0; i < destinationTable.Columns.Count; i++)
                            {    //check if destination Column Exists in Source table
                                if (dtSource.Columns.Contains(destinationTable.Columns[i].ToString()))//contain method is not case sensitive
                                {
                                    int sourceColumnIndex = dtSource.Columns.IndexOf(destinationTable.Columns[i].ToString());//Once column matched get its index
                                    bulkCopy.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
                                }
    
                            }
                            bulkCopy.WriteToServer(destinationTable);
                            bulkCopy.Close();
    

提交回复
热议问题