SQLBulkCopy inserts a new row with NULL values for all columns

后端 未结 1 1709
星月不相逢
星月不相逢 2020-12-22 02:50

I have this code which works fine and loads the excel data into an SQL table. The only problem is that it also inserts a new row with NULL values for all columns.

         


        
相关标签:
1条回答
  • 2020-12-22 03:29

    You could change your query to skip empty rows in excel:

    "SELECT * FROM [" + Path.GetFileName(excelPath) + "] WHERE [Employee] IS NOT NULL"
    

    This should avoid that empty rows are added to the DataTable. Of course you could also remove them later but it would be less efficient. For example:

    dtExcelData = dtExcelData.AsEnumerable()
        .Where(r => !String.IsNullOrEmpty(r.Field<string>("Employee")))
        .CopyToDataTable();
    
    0 讨论(0)
提交回复
热议问题