简体   繁体   English

如何删除记录?

[英]How to delete a record?

I have records in table1, if the records exist, it must copy into table2. 我在table1中有记录,如果记录存在,则必须将其复制到table2中。 I want to delete those records in a table1 once all the records are copied into another table2. 一旦所有记录都复制到另一个table2中,我想删除table1中的那些记录。 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. 我仍然是数据库的初学者,并进行了一些研究,我在d Internet上找到了一些有关如何与数据库连接的教程,并且代码易于理解,因此我想到了这个程序。该代码仅用于复制部分,我仍然缺少删除部分。 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. 我在msdn中找到了2个参考,但是我不确定并且对给出的代码不理解。


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"; 如果我将查询替换为://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? 我需要考虑EOF的事情吗? DO i need to use DataGridView? 我需要使用DataGridView吗? Becoz the code i did didn't use DataGridView at all. 因为我的代码根本没有使用DataGridView。 i dont want the records to be displayed, i just want it to running behind. 我不希望显示记录,我只希望它在后面运行。

No you do not need a worry about EOF or using a DataGridView. 不,您不需要担心EOF或使用DataGridView。 Just as you can use an ExecuteNonQuery method to insert multiple rows you can also do the same when using DELETE. 正如可以使用ExecuteNonQuery方法插入多行一样,使用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. 诸如INSERT,UPDATE和DELETE之类的数据操作语句不会生成结果集,因此通常应使用ExecuteNonQuery来运行它们。 All the data manipulation runs in the database server engine. 所有数据操作都在数据库服务器引擎中运行。

DELETE FROM tblSend WHERE id = 5;

This will delete all rows that match the WHERE condition. 这将删除所有符合WHERE条件的行。

I am not sure I understand the relevance of the DataGridView. 我不确定我是否了解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. 您只需要发出一次delete查询,其余的将自动发生,假设您的数据绑定正确。

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. 要从表1中删除多个记录,您必须进行遍历表并进行比较的循环。

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;
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 如果要将身份密钥再次设置为0,请使用此代码

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

Then press F5, 然后按F5,

If I understand correctly you need all data from table1 in table2 and then delete table1. 如果我正确理解,则需要table2中table1中的所有数据,然后删除table1。

Options 选件

1) It you need it once you could rename table1 to table2 and recreate table1 1)一旦可以将table1重命名为table2并重新创建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 2)假设您有类似主键的内容,请进行单独的复制和删除

-- 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. 3)使用C#进行操作,但是对我来说这似乎是一个数据库问题,我不会拖拉所有记录到客户端然后将它们扔回去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM