Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
in access database, he id field i want to sort it again, such as if the id filed was 1,22,30 .
i want make it 1,2,3
this the code .
C#
OleDbConnection h5 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\IJC Database.accdb");
            OleDbConnection h6 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\\users\\IJC Database.accdb");
            OleDbCommand cmd5;
            OleDbCommand cmd6;
            h5.Open();
            cmd5 = new OleDbCommand("select count(ID) from ijc"+";",h5);
            int countID = (int)cmd5.ExecuteScalar();
            h5.Close();
            h6.Open();
            for (int i = 1; i <= countID; i++)
            {
                cmd6 = new OleDbCommand("update ijc set ID='"+i+"';",h6);
                cmd6.ExecuteNonQuery();
            
            }
            h6.Close();

but how can i move to the next id...
Posted
Updated 24-Jun-13 8:48am
v3
Comments
Mike Meinz 24-Jun-13 14:48pm    
1. Each time you execute the UPDATE statement, you are changing all rows in the table. Usually there is a WHERE clause on an UPDATE statement to specify which row(s) should be changed.

2. You do not have a SELECT statement to select rows. Your SELECT statement selects an aggregate function value. A single value does not need to be sorted.

3. Rows are not kept sorted in the database. Rows are sorted after they are retrieved in a SELECT statement before they are returned to your application.

4. You do not need two OleDbCommand variables. You can execute both your SELECT and UPDATE statement using the same OleDbCommand variable.
[no name] 24-Jun-13 14:49pm    
What do you mean "move to the next id"? All you are doing is setting all of the IDs in the table to the value of "i" in a loop. At the very least, you need to figure out what a WHERE clause does.
loai_maane 24-Jun-13 14:54pm    
i mean ... in my database when i add data the id simply is 1,2,3....right??
but when i remove one row such as row number 2 the id be wrong so i try to write a code to make the id filed again 1,2,3...
i do not know how can explain, but i hope you understand me.
Mike Meinz 24-Jun-13 14:56pm    
Why would you do that?
loai_maane 24-Jun-13 15:00pm    
because when i finish my project, i want to print a report include that data in the access database

You may have to clarify what you are really trying to do. If you are using the ID as a sorting column and are trying to adjust the ID's, then you are really going against what the ID column is used for.

Look at it this way, if you have another table in the database (call it Table2) that references (call it Table1) by ID, and you update the ID's in Table 1 but not the ones in Table2, then you've broken every relationship in the two tables.

If you only want to use the ID to sort the results, your select query should look something like this:

SQL
SELECT * FROM ijc ORDER BY ID


Which will order by ID. The last thing you should ever do in a database is re-order ID's, id's are just a number, don't put too much emphasis on the order.
 
Share this answer
 
Do you want to "re-use" ID values which got free by e.g. deleting some rows from the database? That's a useless task! The ID is an automatically generated value, its only purpose is to identify a row. Filling up the gaps between the values won't alter peformance of your database.
 
Share this answer
 
I am sure it will be working on it.

string query = "select * from " + "TableName" + " Order by" + " ID";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900