Данное сопоставление столбцов не совпадает ни с одним столбцом в источнике или пункте назначения.
Files =Directory.GetFiles(GlobalLibrary.PickupInboundDirectory); FileArray = FilesList.GetIBPckpList(Files, dt); ic.ImportCSV("Inbound_Pickup", FileArray, ','); FileArray.Clear(); Application.Exit();
public DataTable ImportCSV(string Type, List<string> FilesList, char Separator) { DataTable dtIO= new DataTable(); AddDefaultFirstRecord(Type, ref dtIO); foreach (string file in FilesList) { string[] Lines = File.ReadAllLines(file); for(int i=0;i<Lines.Length;i++) { string[] FieldValues=Lines[i].Split(Separator); if (FieldValues.Length == 1) FieldValues = Lines[i].Split('|'); if (FieldValues.Length == 1) FieldValues = Lines[i].Split('\t'); //if(FieldValues.Length==1) // FieldValues=Lines[i].Split( try { DataRow dr = Fetch_DR(Type, FieldValues, dtIO); dtIO.Rows.Add(dr); } catch (Exception ex) { LogWriter.LogWrite("Error", ex.Message + " Type: " +Type+" "+ Lines[i]); } } } DAL_BulkIO dal = new DAL_BulkIO(); dal.Insert_BulkIO(Type,dtIO); return dtIO; }
private void AddDefaultFirstRecord(string Type,ref DataTable dtOutboundPick) { //creating DataTable DataTable dt = new DataTable(); DataRow dr; switch (Type) { case "Inbound_Pickup": dtOutboundPick.TableName = "Inbound_Pickup"; //creating columns for DataTable dtOutboundPick.Columns.Add(new DataColumn("sd_awb", typeof(string))); dtOutboundPick.Columns.Add(new DataColumn("sd_dtpu", typeof(DateTime))); dtOutboundPick.Columns.Add(new DataColumn("sd_acct", typeof(string))); dtOutboundPick.Columns.Add(new DataColumn("sd_bill", typeof(string))); dtOutboundPick.Columns.Add(new DataColumn("sd_gbl_prod", typeof(string))); dtOutboundPick.Columns.Add(new DataColumn("sd_orig", typeof(string))); dtOutboundPick.Columns.Add(new DataColumn("sd_dest", typeof(string))); dtOutboundPick.Columns.Add(new DataColumn("sd_shpr", typeof(string))); dtOutboundPick.Columns.Add(new DataColumn("sd_cnsg", typeof(string))); dtOutboundPick.Columns.Add(new DataColumn("sd_pcs", typeof(double))); dtOutboundPick.Columns.Add(new DataColumn("sd_wght", typeof(double))); dtOutboundPick.Columns.Add(new DataColumn("sd_dwght", typeof(double))); dtOutboundPick.Columns.Add(new DataColumn("sd_stel", typeof(string))); dtOutboundPick.Columns.Add(new DataColumn("sd_czip", typeof(string))); dtOutboundPick.Columns.Add(new DataColumn("PL_CHKPT_DATE", typeof(DateTime))); break; } dr = dt.NewRow(); dt.Rows.Add(dr); }
public DataRow Fetch_DR(string Type, string[] FieldValues, DataTable dt) { DataRow dr = dt.NewRow(); switch (Type) { case "Inbound_Pickup": { dr["sd_awb"] = FieldValues[0].Trim(); dr["sd_dtpu"] = ConvertToDt(FieldValues[1].Trim()); dr["sd_acct"] = FieldValues[2].Trim(); dr["sd_bill"] = FieldValues[3].Trim(); dr["sd_gbl_prod"] = FieldValues[4].Trim(); if (FieldValues[4].Trim() == "") { throw new Exception("Product should not be emtpy"); } dr["sd_orig"] = FieldValues[5].Trim(); dr["sd_dest"] = FieldValues[6].Trim(); dr["sd_shpr"] = FieldValues[7].Trim(); dr["sd_cnsg"] = FieldValues[8].Trim(); dr["sd_pcs"] = FieldValues[9].Trim(); dr["sd_wght"] = FieldValues[10].Trim(); dr["sd_dwght"] = FieldValues[11].Trim(); dr["sd_stel"] = FieldValues[12].Trim(); dr["sd_czip"] = FieldValues[13].Trim(); dr["PL_CHKPT_DATE"] = ConvertToDt(FieldValues[14].Trim()); } break; } return dr; }
public bool Insert_BulkIO(string Type, DataTable dtIO) { try { if (dtIO.Rows.Count > 0) { using (SqlConnection con = new SqlConnection(consString)) { using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con)) { //Set the database table name sqlBulkCopy.DestinationTableName = "dbo."+Type; //[OPTIONAL]: Map the DataTable columns with that of the database table foreach (DataColumn col in dtIO.Columns) { sqlBulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName); } con.Open(); sqlBulkCopy.WriteToServer(dtIO); con.Close(); } } } return true; } catch (Exception ex) { LogWriter.LogWrite("Error","Type :"+Type+" Exception: "+ex.Message); return false; } }
USE [DSR_N] GO /****** Object: Table [dbo].[Inbound_Pickup] Script Date: 02/21/2018 15:51:01 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Inbound_Pickup]( [ sd_awb] [nvarchar](255) NULL, [sd_dtpu] [datetime] NULL, [sd_acct] [nvarchar](255) NULL, [sd_bill] [nvarchar](255) NULL, [sd_gbl_prod] [nvarchar](255) NULL, [sd_orig] [nvarchar](255) NULL, [sd_dest] [nvarchar](255) NULL, [sd_shpr] [nvarchar](255) NULL, [sd_cnsg] [nvarchar](255) NULL, [sd_pcs] [float] NULL, [sd_wght] [float] NULL, [sd_dwght] [float] NULL, [sd_stel] [nvarchar](255) NULL, [sd_czip] [nvarchar](255) NULL, [PL_CHKPT_DATE] [datetime] NULL ) ON [PRIMARY] GO
Что я уже пробовал:
1. Я проверил тип данных каждого столбца и сопоставленный с SQL
2. Я проверил имена столбцов
3. я проверил название таблицы.
4. я также проверил чувствительность к регистру имен столбцов.
Выглядит все хорошо, но не понимаю, почему система выдает ошибку на линии sqlBulkCopy.WriteToServer(дтио);