一个快速的方法来一次删除数据表中的所有行 [英] A fast way to delete all rows of a datatable at once

查看:187
本文介绍了一个快速的方法来一次删除数据表中的所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除一个DataTable中的所有行。
我用的是这样的:

I want to delete all rows in a datatable. I use something like this:

foreach (DataRow row in dt.Rows)
{
  row.Delete();
}
TableAdapter.Update(dt);



它的工作原理很好,但它需要大量的时间,如果我有很多行。
有什么办法可以一次删除所有行?

It works good but it takes lots of time if I have much rows. Is there any way to delete all rows at once?

推荐答案

如果您运行的是你对一个SQLServer数据库的代码然后结果
使用此命令

If you are running your code against a sqlserver database then
use this command

string sqlTrunc = "TRUNCATE TABLE " + yourTableName
SqlCommand cmd = new SqlCommand(sqlTrunc, conn);
cmd.ExecuteNonQuery();

这将是最快的方法,并会删除一切从你的表和身份计时器归零。

this will be the fastest method and will delete everything from your table and reset the identity counter to zero.

截断关键字由其他RDBMS也支持。

The TRUNCATE keyword is supported also by other RDBMS.

这篇关于一个快速的方法来一次删除数据表中的所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆