本文介绍了Executenonquery()返回-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中插入了一些值。我正在使用visual C#和sql server.Here当我插入值时,我在执行nonquery()时得到-1,并且数据库没有更新。我通过调试检查了每个值。但是找不到它来的哪一行的错误。所以我怎么能找到



我尝试了什么:



我用谷歌搜索但未能得到合适的解决方案。

I am inserting some values into databse. I am using visual C# and sql server.Here when I am inserting values I am getting -1 at execute nonquery() and database is not updated. I checked every value by debugging. but Failed to find error in which line it is coming.So how can I find that

What I have tried:

I googled but failed to get suitable solution.

推荐答案


CREATE TABLE NoCountTable (
   col1 int
);
GO





测试程序



Test procedures

CREATE PROCEDURE NoCountProcedure AS
BEGIN
  SET NOCOUNT ON;
  INSERT INTO NoCountTable VALUES (1);
END;
GO

CREATE PROCEDURE CountProcedure AS
BEGIN
  SET NOCOUNT OFF;
  INSERT INTO NoCountTable VALUES (1);
END;
GO

CREATE PROCEDURE RollbackProcedure AS
BEGIN
  SET NOCOUNT ON;
  BEGIN TRANSACTION
     INSERT INTO NoCountTable VALUES (1);
  ROLLBACK;
END;
GO

CREATE PROCEDURE ExceptionProcedure AS
BEGIN
  SET NOCOUNT ON;
  BEGIN TRANSACTION
     INSERT INTO NoCountTable VALUES (1/0);
  ROLLBACK;
END;
GO





来电计划



Calling program

int result;

using (SqlConnection connection = new SqlConnection()) {
   connection.ConnectionString = "server=.\\<instance_name_here>;integrated security=true";
   using (SqlCommand command = new SqlCommand()) {
      command.Connection = connection;
      try {
         connection.Open();

         command.CommandText = "NoCountProcedure";
         result = command.ExecuteNonQuery();
         Debug.WriteLine(string.Format("{0} returned {1}",
                         command.CommandText,  result));

         command.CommandText = "CountProcedure";
         result = command.ExecuteNonQuery();
         Debug.WriteLine(string.Format("{0} returned {1}",
                         command.CommandText, result));

         command.CommandText = "RollbackProcedure";
         result = command.ExecuteNonQuery();
         Debug.WriteLine(string.Format("{0} returned {1}",
                         command.CommandText, result));

         command.CommandText = "ExceptionProcedure";
         result = command.ExecuteNonQuery();
         Debug.WriteLine(string.Format("{0} returned {1}",
                         command.CommandText, result));
      } catch (System.Exception exception) {
         Debug.WriteLine(string.Format("Exception occurred {0}: {1}",
                         exception.Source, exception.Message));
      }
   }
}





程序的输出是



The output from the program is

NoCountProcedure returned -1
CountProcedure returned 1
RollbackProcedure returned -1
Exception thrown: 'System.Data.SqlClient.SqlException' in System.Data.dll
Exception occurred .Net SqlClient Data Provider: Divide by zero error encountered.
The statement has been terminated.





但正如@ Pete-OHanlon指出的那样,这只是解释了不同的可能性你还没有发布任何代码。



But as pointed out by @Pete-OHanlon, this is merely explaining different possibilities since you haven't posted any code.



这篇关于Executenonquery()返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:21