c# - Create Table with Parameters -


create excel table, using oledb:

oledbcommand oledbcommand = new oledbcommand(); oledbcommand.connection = oledbconnection;  string commandtext = "create table" + " [" + sheetmodel.sheet.name + "] "; commandtext += "(";  (int index = 0; index < spalten; index++) {     string _header = sheetmodel.dt1.rows[heaader].itemarray[index].tostring();      oledbcommand.parameters.add(new oledbparameter("@var" + (index + 1).tostring(), _header));      if (index > 0)     {         commandtext += ", ";     }      commandtext += "@var" + index.tostring() + " varchar"; } commandtext += ");";   try {     oledbcommand.commandtext = commandtext;     oledbcommand.executenonquery();     oledbcommand.parameters.clear(); } catch (exception exception) {     messagebox.show(exception.message);     return; }   

the result excel table is,

@var0 @var1 @var2 @var3 @var4 @var5

but should this:

"preis/stk. euro" "stk." "" "produkt" "artikelmerkmale" "sonstige"

where mistake?
code working fine inserting values.

as learningnew stated, you're providing @var+(index + 1) inside loop column headers.

to correct this, include switch condition provide actual column header based on current iteration cycle.

to so, change line

oledbcommand.parameters.add(new oledbparameter("@var" + (index + 1).tostring(), _header)); 

to (untested):

string colheader = string.empty;  // variable column header  switch (index) {     case 0:         colheader = "preis/stk. euro";     case 1:         colheader = "stk.";     case 2:         colheader = "produkt";     default:         break; }  // using string variable here set actual column header oledbcommand.parameters.add(new oledbparameter(colheader, _header)); 

Comments

Popular posts from this blog

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -

YouTubePlayerFragment cannot be cast to android.support.v4.app.Fragment -