Inserting into Excel using OleDb overwriting the wrong row multiple times -
Inserting into Excel using OleDb overwriting the wrong row multiple times -
i trying insert existing excel spreadsheet using oledbconnection
. noticing when insert, though specify sheet offset, still writing wrong row. worse, every record getting written same row! here code:
static void main(string[] args) { string destination = @"c:\publish\output.xlsx"; file.copy(@"c:\publish\template.xlsx", destination, true); string connectionstring = getconnectionstring(destination); using (oledbconnection connection = new oledbconnection(connectionstring)) { connection.open(); using (oledbcommand command = connection.createcommand()) { command.commandtext = @" insert [advertisers$a3:ac] values ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )"; // spit out 29 bogus values (int = 0; != 29; ++i) { addparameter(command, "value = " + i); } // insert 1200 records (int = 0; != 1200; ++i) { command.executenonquery(); } } } } private static string getconnectionstring(string path) { oledbconnectionstringbuilder builder = new oledbconnectionstringbuilder(); builder.provider = "microsoft.ace.oledb.12.0"; builder.add("extended properties", "excel 12.0 xml"); builder.datasource = path; homecoming builder.connectionstring; } private static void addparameter(oledbcommand command, object value) { oledbparameter parameter = command.createparameter(); parameter.parametername = "?"; parameter.value = value == null ? dbnull.value : value; command.parameters.add(parameter); }
i copied same code different console application , worked expected. hmm? after comparing 2 projects while, noticed under project properties, under build
, prefer 32-bit
check box checked. unchecking fixed issue!
excel oledb sql-insert
Comments
Post a Comment