473,399 Members | 3,656 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Sequential read and update of table

Using OleDb, is it possible to sequentially read a (Access) table and update some records in it?

The read (ExecuteReader) works fine, but when I attempt to update, I get a message telling me I have to close the reader connection first before updating, which defeats the purpose of the reader.

The update examples in the documentation use select then update. In my application the records must be read in chronological order.

Thanks
Oct 14 '09 #1
5 4323
cloud255
427 Expert 256MB
Could you post the problematic code for us to have a look at?
Oct 15 '09 #2
private void btnResetAll_Click(object sender, System.EventArgs e)
{
selectQuery = "SELECT ";
selectQuery += "* ";
selectQuery += "FROM ";
selectQuery += "ScheduleR";
sqlCommand = new OleDbCommand(selectQuery, MyConn);
OleDbDataReader myReader;
myReader = sqlCommand.ExecuteReader(CommandBehavior.SingleRow );
readCount = 0;
updateCount = 0;
tableName = "ScheduleR";
while (myReader.Read())
{
schSeq = myReader.GetInt16(0);
schLine = myReader.GetInt16(1);
schSeg = myReader.GetString(2);
schDir = myReader.GetInt16(3);
schRun = myReader.GetInt16(4);
schCar = myReader.GetString(5);
schStnStart = myReader.GetString(6);
schTimeStart = myReader.GetDateTime(7);
schTimeEnd = myReader.GetDateTime(8);
schStnEnd = myReader.GetString(9);
schAssignment = myReader.GetInt16(10);
schPart = myReader.GetString(11);
schService = myReader.GetString(12);
schTrip = myReader.GetInt16(13);
ProcessData();
Console.WriteLine(schLine.ToString() + schSeg + schDir.ToString());
readCount += 1;
}
listBoxOpsLog.Items.Add ("Records Read =" + readCount.ToString());
listBoxOpsLog.Items.Add ("Records Updated =" + updateCount.ToString());
myReader.Close();
MyConn.Close();
}

private void ProcessData()
{
if (schAssignment != 0)
{
myDA = new OleDbDataAdapter();
updateCommand = new OleDbCommand();
updateCommand.CommandText = "UPDATE ScheduleR SET Assignment=0";
updateCommand.Connection = MyConn;
myDA.UpdateCommand = updateCommand;
myDA.UpdateCommand.ExecuteNonQuery();
updateCount += 1;
}
}


The objective to to sequentially read the table and set the field "Assignment" to zero.
The error happens on ExecuteNonQuery();
The text of the error is:
"There is already an open DataReader associated with this Connection which must be closed first."

I am new to C# and OleDb.

Thanks
Oct 15 '09 #3
cloud255
427 Expert 256MB
Hmmm, I would approach this slightly differently.

You can simply select all the rows from the appropriate table:

Expand|Select|Wrap|Line Numbers
  1. private void btnResetAll_Click(object sender, System.EventArgs e)
  2. {
  3. string selectQuery = "SELECT * FROM ScheduleR";
  4. var sqlCommand = new OleDbCommand(selectQuery, MyConn);
  5. OleDbDataReader myReader;
  6. myReader = sqlCommand.ExecuteReader(); //this will get all the rows
  7.  
  8. while (myReader.Read())
  9. {
  10.  schSeq = myReader.GetInt16(0);
  11.   schLine = myReader.GetInt16(1);
  12.   schSeg = myReader.GetString(2);
  13.   schDir = myReader.GetInt16(3);
  14.   schRun = myReader.GetInt16(4);
  15.   schCar = myReader.GetString(5);
  16.   schStnStart = myReader.GetString(6);
  17.   schTimeStart = myReader.GetDateTime(7);
  18.   schTimeEnd = myReader.GetDateTime(8);
  19.   schStnEnd = myReader.GetString(9);
  20.   schAssignment = myReader.GetInt16(10);
  21.   schPart = myReader.GetString(11);
  22.   schService = myReader.GetString(12);
  23.   schTrip = myReader.GetInt16(13);
  24.   Console.WriteLine(schLine.ToString() + schSeg + schDir.ToString());
  25.   readCount += 1
  26. }
  27.   myReader.Close();
  28.   MyConn.Close();
  29.  
  30.   ProcessData(); //only called once
  31.  
  32.   listBoxOpsLog.Items.Add ("Records Read =" + readCount.ToString());
  33.   istBoxOpsLog.Items.Add ("Records Updated =" + updateCount.ToString());
  34. }
  35.  
  36.  
You can update all the values with one SQL command since you are setting everything that is not 0 equal to 0.

Expand|Select|Wrap|Line Numbers
  1. private void ProcessData()
  2. {
  3.   //There is no need to check if the schAssignment != 0 as you are setting all rows schAssignment value to 0;
  4.   myDA = new OleDbDataAdapter();
  5.   updateCommand = new OleDbCommand();
  6.   updateCommand.CommandText = "UPDATE ScheduleR SET Assignment=0";
  7.  
  8.   MyConn.Open();
  9.  
  10.   updateCommand.Connection = MyConn;
  11.   myDA.UpdateCommand = updateCommand;
  12.   updateCount = myDA.UpdateCommand.ExecuteNonQuery();
  13. }
PS Please use code tags (# symbol in toolbar) when posting code, this makes code easier to read.
Oct 15 '09 #4
This program is merely a shell for the final version, just to see if it is possible to sequentially read a table and update some of the records.

In the final version, complex logic must be used to check dates, times, and references to data in other tables in order to determine which records to update, and the update will be setting the assignment number to a non-zero number.

The logic will be more complex than can be handled by a SELECT.

The table must be read in sequential order since the data refers to events in chronological order.

Thanks
Oct 15 '09 #5
cloud255
427 Expert 256MB
@harmikm
Uh, ok. Is it not possible for you to use an 'order by' statement in the query to get the data in a specific order?

If you are going to be doing data inspection and manipulation I suggest you define a data structure (a List<> of some sort of custom object for example) to hold the data in memory. It will be easier to manipulate this structure and save the results back to the database again instead of doing it one record at a time.

So basically: Load everything you will need into memory using a select statement, update data which must be changed and finally persist those changes back to the database.
Oct 16 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Chris Hall | last post by:
The records in my database are displayed in a form as follows: %> <form action="report-ammend1.42.asp" method="post"name="form"> <table border=1> <% x = 1
6
by: cjm | last post by:
I need to group records and assign a setid to the group. I have a table with data that looks like this ColA ColB 94015 01065 94016 01065 94015 01085 94015 01086 33383 00912 32601 00912
6
by: Jenn L | last post by:
I have a database that is pre-populated with sequential part numbers. As people reserve the parts I update a flag to show the # is no longer available. Now they want the ability to take out a...
9
by: cyrus.kapadia | last post by:
Let's say I have the following table: entry product quality 1 A 80 2 A 70 3 A 80 4 B 60 5 B 90 6 ...
7
by: GAVO. | last post by:
Hello every one I have a database with a form called "frmOrders" on that for I need to create a sequential number for each city apart from the original autonumber. So the table "tblorders" would...
14
by: amywolfie | last post by:
Hi All: I know this is simple, but I just can't seem to get there: I need to sort a table by a text field (txtDescription), then assign sequential numbers to the field SEQUENCE in table. ...
2
by: John | last post by:
Hi I need to assign sequential invoice numbers to orders starting from the last highest number + 1. I have tried the following code; UPDATE Orders SET Orders. = DMax("","Orders")+1 WHERE...
19
by: eric.nave | last post by:
this is a slight change to a fequently asked question around here. I have a table which contains a "sortorder" column where a user can specify some arbitrary order for records to be displayed in. ...
11
by: Timofmars | last post by:
I'm try to Unload DB2 data from a table into a record sequential file on NT. I can an unload on Mainframe, but it doesn't seem to be an option in NT. In NT, all I can do is export/import. I can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.