| | |
Updating columns in an access database using c#
Please support our C# advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
![]() |
•
•
Join Date: Dec 2007
Posts: 1
Reputation:
Solved Threads: 0
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:
Method 2:
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:
c# Syntax (Toggle Plain Text)
static public void WriteDataC(string tableName, string columnName, string[] stringValues) { ADODB.RecordsetClass recordset = new ADODB.RecordsetClass(); ADODB.RecordClass record = new ADODB.RecordClass(); string sqlString = "select " + columnName + " from " + tableName; recordset.Open(sqlString, catalog.ActiveConnection, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockOptimistic, 0); bool noRecords = false; object nil = Type.Missing; if (recordset.BOF && recordset.EOF) { noRecords = true; } else { noRecords = false; recordset.MoveFirst(); } for (int iRec = 0; iRec < stringValues.Length; ++iRec) { if (noRecords) { recordset.AddNew(columnName, stringValues[iRec]); recordset.Update(columnName, stringValues[iRec]); } else { recordset.Fields[columnName].Value = stringValues[iRec]; recordset.Update(columnName, stringValues[iRec]); recordset.MoveNext(); } } recordset.Close(); }
Method 2:
C# Syntax (Toggle Plain Text)
static public void WriteDataC2(string tableName, string columnName, int[] varValues) { connection.Open(); string sqlString = "select " + columnName + " from " + tableName; OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sqlString, connection); OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(dataAdapter); DataTable datatable = new DataTable(); dataAdapter.Fill(datatable); for (int iRec = 0; iRec < varValues.Length; ++iRec) { DataRow datarow = datatable.NewRow(); datarow[columnName] = varValues[iRec]; datatable.Rows.Add(datarow); } dataAdapter.Update(datatable); connection.Close(); }
![]() |
Similar Threads
- database questions I have (Database Design)
- Using VB to compile columns in MS Access (Visual Basic 4 / 5 / 6)
- Error connecting to database (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: C# Nesting
- Next Thread: datagrid hyperlink
| Thread Tools | Search this Thread |
.net access algorithm animation array bitmap box c# check checkbox client combobox control conversion csharp customactiondata cyclethruopenforms data database datagrid datagridview dataset date/time datetime datetimepicker degrees directrobot dll draganddrop drawing encryption enum excel file filename files finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile globalization gtk hash image input install installer java list math mono mouseclick mysql operator outlook2003 panel path photoshop picturebox pixelinversion pixelminversion post print programming radians regex remoting richtextbox save server sleep snooze socket sql sql-server statistics string table tables tcp text textbox thread time timer timespan update usercontrol usercontrols users validate validation visualstudio webcam wia wpf xml





