Updating columns in an access database using c#

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2007
Posts: 1
Reputation: arnaki is an unknown quantity at this point 
Solved Threads: 0
arnaki arnaki is offline Offline
Newbie Poster

Updating columns in an access database using c#

 
0
  #1
Jan 7th, 2008
Dear c-sharpers,

I'm relatively new developing code using c#. I created an access database with a table using ADOX. I want to fill the table with about 2 million records and something like 20 columns of different types (int, double, string). So far I used different methods to fill this table with data:
1) all columns at once using "INSERT INTO" statements and followed by an oledbcommand.executenonquery();
2) all columns at once using ADODB recordsets.
The second way is much faster than the first method if one wants to create thousands of records. For my problem in which the columntypes and number of columns are known in advance this is sufficient.

However, I also want to generalise it in a library so Fortran-users in our company can start using it as a fortran library. The easiest way (I think) is to use a routine that fills only one column with data. For this I also use two different ways, which both fail. I select one column and add a new row if this is the first time a column is filled with data. The first method uses recordsets again, and the second method uses the OleDbDataAdapter and a dataTable (see code below). Unfortunately, in both cases the size of the Access-file explodes. Does anyone have suggestions on how to taggle this problem?

Thanks in advance!

Method 1:
  1. static public void WriteDataC(string tableName, string columnName, string[] stringValues)
  2. {
  3. ADODB.RecordsetClass recordset = new ADODB.RecordsetClass();
  4. ADODB.RecordClass record = new ADODB.RecordClass();
  5. string sqlString = "select " + columnName + " from " + tableName;
  6. recordset.Open(sqlString, catalog.ActiveConnection, ADODB.CursorTypeEnum.adOpenForwardOnly,
  7. ADODB.LockTypeEnum.adLockOptimistic, 0);
  8.  
  9. bool noRecords = false;
  10. object nil = Type.Missing;
  11. if (recordset.BOF && recordset.EOF)
  12. {
  13. noRecords = true;
  14. }
  15. else
  16. {
  17. noRecords = false;
  18. recordset.MoveFirst();
  19. }
  20.  
  21. for (int iRec = 0; iRec < stringValues.Length; ++iRec)
  22. {
  23. if (noRecords)
  24. {
  25. recordset.AddNew(columnName, stringValues[iRec]);
  26. recordset.Update(columnName, stringValues[iRec]);
  27. }
  28. else
  29. {
  30. recordset.Fields[columnName].Value = stringValues[iRec];
  31. recordset.Update(columnName, stringValues[iRec]);
  32. recordset.MoveNext();
  33. }
  34. }
  35.  
  36. recordset.Close();
  37. }

Method 2:
  1. static public void WriteDataC2(string tableName, string columnName, int[] varValues)
  2. {
  3. connection.Open();
  4. string sqlString = "select " + columnName + " from " + tableName;
  5. OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sqlString, connection);
  6. OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(dataAdapter);
  7.  
  8. DataTable datatable = new DataTable();
  9.  
  10. dataAdapter.Fill(datatable);
  11.  
  12. for (int iRec = 0; iRec < varValues.Length; ++iRec)
  13. {
  14. DataRow datarow = datatable.NewRow();
  15. datarow[columnName] = varValues[iRec];
  16. datatable.Rows.Add(datarow);
  17. }
  18. dataAdapter.Update(datatable);
  19. connection.Close();
  20. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC