Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Given the following code that connects to an Excel File (as a DB) and reads (works great) and writes (doesn't work - this is the problem).

 

The Excel file itself (C:\myData.XLS Sheet1) looks like this:

row(1): Client1 Assignment1 RUNNING

row(2): Client1 Assignment2 PAUSED

row(3): Client2 Assignment1 FINISHED

(spaces delimited between columns)

 

using System.Data;
using System.Data.OleDb;

DataSet DS;
OleDbDataAdapter MyCommand;
OleDbConnection MyConnection;

MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; data source=C:\myData.XLS; Extended Properties=Excel 8.0;");

// Select the data from Sheet1 of the workbook.
MyCommand = new System.Data.OleDb.OleDbDataAdapter("select * from [sheet1$]", MyConnection);

// READ from Excel DB
DS = new DataSet();
MyCommand.Fill(DS);
MyConnection.Close();

// Write to Excel DB
MyCommand = new System.Data.OleDb.OleDbDataAdapter(insert into [sheet1$] ([CLIENTS], [ASSIGNMENTS], [sTATUS]) values( '" + cbClient.Text + "', '" + cbAssignment.Text + "', 'PAUSED')", MyConnection);

 

So as previouslly mentioned this code READS values from my Excel File correctly (DataSet ds is properly populated) HOWEVER it doesn't seem to WRITE (the INSERT INTO). Don't get me wrong - this doesn't generate any errors and seems to execute fine BUT it doesn't actually make/save the changes to the EXCEL (.xls) file. IS there something I am missing? Do I need to confirm/save the changes? Am I missing something with my MyCommand.?? Commit changes?

 

Any help/hints would be GREATLY appreciated.. Thanks,

Posted (edited)

Try:

 

using System.Data;
using System.Data.OleDb;

DataSet DS;
OleDbDataCommand MyCommand;
OleDbDataAdapter MyAdapter;
OleDbConnection MyConnection;

MyConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; data source=C:\myData.XLS; Extended Properties=Excel 8.0;");

// Select the data from Sheet1 of the workbook.
MyAdapter = new System.Data.OleDb.OleDbDataAdapter("select * from [sheet1$]", MyConnection);

// READ from Excel DB
DS = new DataSet();
MyAdapter.Fill(DS);
MyConnection.Close();

// Write to Excel DB
MyCommand = new System.Data.OleDb.OleDbCommand("insert into [sheet1$] ([CLIENTS], [ASSIGNMENTS], [sTATUS]) values( '" + cbClient.Text + "', '" + cbAssignment.Text + "', 'PAUSED')", MyConnection);
MyCommand.ExecuteNonQuery();

 

:)

Edited by mark007

Please check the Knowledge Base before you post.

"Computers are useless. They can only give you answers." - Pablo Picasso

The Code Net

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...