c# - store sheetname in table -


i have following code . can extract data data existing in sheets in workbook , name of sheet

foreach (var sheetname in getexcelsheetnames(connectionstring))             {                 using (oledbconnection con = new oledbconnection(connectionstring))                 {                      var datatable = new datatable();                     string query = string.format("select {0} sheetname, * [{0}]", sheetname);                     con.open();                     oledbdataadapter adapter = new oledbdataadapter(query, con);                     adapter.fill(datatable);                     ds.tables.add(datatable);                 }             } 

then write following code store data in table in sql server

if (ds.tables.count > 0)             {                 foreach (datatable dt in ds.tables)                 {                      using (sqlconnection con = new sqlconnection(consstring))                         {                             con.open();                             (int = 2; < dt.rows.count; i++)                             {                                  (int j = 1; j < dt.columns.count; j += 3)                                 { //here problem  sqlcommand command = new sqlcommand( should write? ) ;                                      command.executenonquery();                                 }                             }                             con.close();                         }                     } 

my problem how store sheetname in table [obj ca]?

i think your

"...values('sheetname" + dt.rows[0][j].tostring() + "' ) 

should be

"...values('sheetname', '" + dt.rows[0][j].tostring() + "' ) 

since try insert 2 values didn't seperate them comma.

but better way, use parameterized queries. kind of string concatenations open sql injection attacks.

var command = new sqlcommand(@"insert [obj ca] (sheetname, [rayon])                                values('sheetname', @rayon"), con);  (int = 2; < dt.rows.count; i++) {       (int j = 1; j < dt.columns.count; j += 3)       {              command.parameters.clear();                      command.parameters.addwithvalue("@rayon", dt.rows[0][j].tostring());           command.executenonquery();        } } 

by way, since didn't know column types, used addwithvalue example don't. this method may generate unexpected results sometimes. use add overload specify parameter type , it's size.

also suspect should change column definition order (sheetname, [rayon]) in insert statement.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -