Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 10th, 2003, 11:04 AM
Authorized User
 
Join Date: Jun 2003
Location: Rio de Janeiro, Rio de Janeiro, Brazil.
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to delete two or more lines in a Table???


I have a problem with the code below.
I have a table that has a column called REGISTRO. Some times i have two or more lines with the same value in the column REGISTRO.
I want to remove all lines in the table with an specific value. For example all lines with REGISTRO value equals MNRJ02.
The program executes all of the commands and dont show me any error.
In the end when I open the table to verify the alterations, the lines had not been excluded and still they are in the table.
If somebody will be able to help me
Thanks

My code Below

thisAdapter = new OleDbDataAdapter ("SELECT * FROM lig_det ORDER BY registro", dbBanco.thisConnection);

                thisBuilder = new OleDbCommandBuilder (thisAdapter);

                thisDataSet = new DataSet ();

                thisAdapter.Fill (thisDataSet, "lig_det");

                //percorrendo toda a tabela LIG_DET para achar os registros que devem
                //ser eliminados
                for (int i = 0; i < thisDataSet.Tables ["lig_det"].Rows.Count; i++)
                {
                    if (registroSgc == thisDataSet.Tables ["lig_det"].Rows [i]["registro"].ToString ())
                    {

                        thisDataSet.Tables ["lig_det"].Rows [i].Delete ();

                        MessageBox.Show ("Estado do registro na tabela " +thisDataSet.Tables ["lig_det"].Rows [i].RowState.ToString ());

                    }//final do if
                }//final do FOR

                thisDataSet.Tables ["lig_det"].AcceptChanges ();

                thisAdapter.Update (thisDataSet, "lig_det");

                MessageBox.Show ("Registro "+registroSgc+" excluído com Sucesso",
                    "HARPIA - REGISTRO EXCLUÍDO",MessageBoxButtons.OK, MessageBoxIcon.Information);
Reply With Quote
  #2 (permalink)  
Old June 11th, 2003, 07:01 PM
Authorized User
 
Join Date: Jun 2003
Location: Gainesville, FL, USA.
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Thomaz.

I'm no expert, but here's my $0.02:

First, is registroSgc is a string, you might want to use the .Equals method to check for equality. Using == should be true only if registroSgc and the reference in the row you are looping on point to the same location.

Secondly, I would try and move the call to AcceptChanges in the inner loop (right after the call to Delete). I don't think this should make any difference, but if all else fails, I'd try that.

Hope this helps.
Reply With Quote
  #3 (permalink)  
Old June 13th, 2003, 06:00 AM
Authorized User
 
Join Date: Jun 2003
Location: Rio de Janeiro, Rio de Janeiro, Brazil.
Posts: 58
Thanks: 0
Thanked 0 Times in 0 Posts
Default

:D
To solve this trouble i use the SQL DELETE command like below.
The DataSet does not work well without a primary key in the table.

The code:

thisAdapter = new OleDbDataAdapter ("DELETE FROM histor WHERE registro = '"+registroSgc+"'", dbBanco.thisConnection);

thisBuilder = new OleDbCommandBuilder (thisAdapter);

thisDataSet = new DataSet ();

thisAdapter.Fill (thisDataSet, "refer");
Reply With Quote
  #4 (permalink)  
Old July 4th, 2003, 02:24 AM
Authorized User
 
Join Date: Jun 2003
Location: , , United Kingdom.
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If you want to let the user remove lines in the table after the query has been ran then you would need to iterate through the dataset checking for a match, and then delete the line. However I haven't gone into detail with this because I am assuming you want to exclude rows with a certain value prior to running the query.

If so you could do it in the SQL by adding a WHERE clause, e.g.:

thisAdapter = new OleDbDataAdapter ("SELECT * FROM lig_det ORDER BY registro WHERE registro <> 'MNRJ02'", dbBanco.thisConnection);

Please be aware that the use of <> conditions within a WHERE clause can reduce performance as every line must be read to check that the value IS NOT the exclusion value. I think in your case you should be ok.

Skin
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete lines from a jtextarea codesamurai Java GUI 0 August 30th, 2006 11:13 AM
Can't Modify or Delete table PatYoda SQL Server 2000 3 December 3rd, 2004 09:41 AM
Change lines in a Table.... cybercross Classic ASP Basics 1 October 2nd, 2004 06:48 AM
Delete Table Teqlump Access 6 September 10th, 2004 04:25 PM



All times are GMT -4. The time now is 12:25 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.