SQL截断问题 [英] Sql Truncate Problem

查看:111
本文介绍了SQL截断问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想删除表的所有行,因此我在项目中使用以下代码:-

Hello,

I want to delete all the rows of a table so i use the following code in my project:-

public int DeleteALL()
      {

          SqlCommand cmd = new SqlCommand(cn);
          cmd.CommandText="Truncate Table TempMonth";
              

          int i = -1;
          try
          {
              i = cmd.ExecuteNonQuery();

          }

          catch (Exception ex)
          {
              this.error = ex.Message;

          }
          return i;
      } 



现在我的概率是....表中的所有行均被删除,但i的值仍为-1.这意味着ExecuteNonQuery()不返回任何内容.



now my prob is.... all the rows from the table is deleted but the value of i is still -1. That means ExecuteNonQuery() doesn''t return anything. what is the prob here anyone please give answer.

推荐答案

查询将返回-1而不是计数.

根据 msdn文档 [For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1."
The query will return -1 rather than a count.

As per msdn documentation[^],
"For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1."


AFAIK:对于UPDATE,INSERT和DELETE语句,ExecuteNonQuery()返回受命令影响的行数.
对于所有其他类型的语句,返回值为-1.

在这里,您使用的是截断而不是UPDATE,INSERT和DELETE.

希望对您有所帮助:)
AFAIK: For UPDATE, INSERT, and DELETE statements, ExecuteNonQuery() returns the number of rows affected by the command.

For all other types of statements, the return value is -1.

And here you are using truncate not UPDATE, INSERT, and DELETE.

hope it helps :)


不,您有点困惑. TRUNCATE TABLE没有返回值,因此ExecuteNonQuery返回-1.
如果将i的初始值设置为1234而不是-1,您将明白我的意思.
No, you are slightly confused. TRUNCATE TABLE does not have a return value, so the ExecuteNonQuery returns -1.
If you set your initial value of i to 1234 instead of -1, you will see what I mean.


这篇关于SQL截断问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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