user147685
user147685

Reputation: 469

How to delete a record?

I have records in table1, if the records exist, it must copy into table2. I want to delete those records in a table1 once all the records are copied into another table2. Im still a beginner in database and with some researches, i found some tutorials on d internet how to connect with database, and the codes easy to understand so i came out with this program.This codes only do the copy part and i'm still lack of the delete part. Can help me figure out how to do the delete part? i found 2 reference in msdn, but i'm not sure and not understand on the codes given.

 
try
{
 //create connection
 System.Data.SqlClient.SqlConnection sqlConnection1 =
    new System.Data.SqlClient.SqlConnection("Data Source=.dbname;Integrated Security=True;User Instance=True");
 //command queries
 System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
 cmd.CommandType = System.Data.CommandType.Text;
 cmd.CommandText = "INSERT INTO tblSend (ip, msg, date) SELECT ip, msg, date FROM tblOutbox";
 cmd.Connection = sqlConnection1;
 sqlConnection1.Open(); //open con
 cmd.ExecuteNonQuery(); //execute query
 sqlConnection1.Close(); //close con
}
catch (System.Exception excep)
{
  MessageBox.Show(excep.Message);
}

If i replace the query into this: //cmd.CommandText = "DELETE tblSend WHERE id = 5"; its only delete one rows. But what if many records involved? Do i need to consider the EOF things? DO i need to use DataGridView? Becoz the code i did didn't use DataGridView at all. i dont want the records to be displayed, i just want it to running behind.

Upvotes: 0

Views: 664

Answers (5)

Janco
Janco

Reputation: 1140

If I understand correctly you need all data from table1 in table2 and then delete table1.

Options

1) It you need it once you could rename table1 to table2 and recreate table1

-- move the records to table 2, ok I assume it does not exist;)
RENAME TABLE table1 TO table2;
-- Create new table1 with same structure as table 2
CREATE TABLE table1 AS SELECT * FROM table2 WHERE 1=2;

2) Do a separate copy and delete assuming you have something like a primary key

-- copy the records
INSERT table2(field1, field2, ...) SELECT field1, field2, ... FROM table1;
-- and delete them 
DELETE FROM table1;

3) Do it using C# but as this seems a database problem to me I would not go that far in pulling all the records to the client and then throwing them back.

Upvotes: 0

MAC
MAC

Reputation: 6577

DELETE FROM tblSend WHERE id = 5;

This is the one solution for deletion a record. If you want to set the identity key to 0 again, use this code

 DBCC CHECKIDENT('tblSend', RESEED, 0); 

Then press F5,

Upvotes: 0

Steven
Steven

Reputation: 19425

What do you mean by "if they exist"? Compared to what?

To delete multiple records from table 1, you have to make a loop which goes through your table and compare.

Pseudo code:

forach (whatever as whut)
 row = select whatever from table1.

 if (whut == row)
  copy row from table 1 to table 2;
  Delete from table 1 where whut.id == row.id;

Upvotes: 0

JYelton
JYelton

Reputation: 36512

DELETE FROM tblSend WHERE id = 5;

This will delete all rows that match the WHERE condition.

I am not sure I understand the relevance of the DataGridView. If it is databound, it will automatically remove the records as well. You only need to issue the delete query once and the rest should happen automatically, assuming you have the databinding correct.

Upvotes: 0

AnthonyWJones
AnthonyWJones

Reputation: 189457

No you do not need a worry about EOF or using a DataGridView. Just as you can use an ExecuteNonQuery method to insert multiple rows you can also do the same when using DELETE.

Data manipulation statements such as INSERT, UPDATE and DELETE do not generate a result set and hence you would normally use ExecuteNonQuery to run them. All the data manipulation runs in the database server engine.

Upvotes: 1

Related Questions