c# - SqlBulkCopy - The given ColumnName does not match up with any column in the source or destination -
i'm trying use sqlbulkcopy copy data sql database table (wrongly) saying columns don't match. match. if use breakpoint see names of columns being mapped, they're correct. error message shows name of column, , correct.
this method. have identical method does work , difference gets column names from. strings containing column names, however, identical.
public static bool manualmapimport(datatable datatable, string table) { if(datatable != null) { sqlconnection connection = new sqlconnection(connectionstring); sqlbulkcopy import = new sqlbulkcopy(connection); import.destinationtablename = "[" + table + "]"; foreach (string s in global.selectedcolumns) { /* s string variable here exact same c.tostring() in other method below */ if (columnexists(table, s)) import.columnmappings.add(s, s); else return false; } connection.open(); import.writetoserver(datatable); //error happens on line connection.close(); return true; } else { return false; } }
this identical, working method:
public static bool automapimport(datatable datatable, string table) { if (datatable != null) { sqlconnection connection = new sqlconnection(connectionstring); sqlbulkcopy import = new sqlbulkcopy(connection); import.destinationtablename = "[" + table + "]"; foreach (datacolumn c in datatable.columns) { if (columnexists(table, c.tostring())) import.columnmappings.add(c.tostring(), c.tostring()); else return false; } connection.open(); import.writetoserver(datatable); connection.close(); return true; } else { return false; } }
if helps, column names are: act_code, act_paid, act_name, act_terminal_code, act_tcustom1, act_tcustom2. these same in database itself. i'm aware sqlbulkcopy mappings case sensitive, , column names indeed correct.
this error message:
an unhandled exception of type 'system.invalidoperationexception' occurred in system.data.dll
additional information: given columnname 'act_code' not match column in data source.
hopefully i'm missing obvious here, , lost.
many thanks.
edit: happening have same problem me, here's how fixed it.
instead of having
manualmapimport()
method near-clone ofautomapimport()
, had loop through columns of datatable , change names, calledautomapimport()
amended datatable, eliminating need try , map plain strings @ all.
according msdn (here), datacolumn.tostring()
method returns "the expression value, if property set; otherwise, columnname property.".
i've found tostring()
method wonky anyway (can change based on current state/conditions), i'd recommend using columnname
property instead, that's trying out of tostring()
.
ok, failing that, i'd have guess problem case-sensitivity in names of columns in source datatable, sqlbulkcopy
very case-sensitive if sql db not. address this, when check see if column exists, should return/use actual string datatable's column list itself, rather using whatever string passed in. should able fix case or accent differences columnsexist routine might ignoring.
Comments
Post a Comment