【问题标题】:C# stored procedure returning -1 RepeatedlyC#存储过程反复返回-1
【发布时间】:2012-11-01 19:03:38
【问题描述】:

我在 SQL Server 上反复测试了这个#blankety-blank# 存储过程,它返回 0 或行数,但在 C# 中我总是得到 -1。我已经调试这个#blank# 几个小时了,而且总是-1。

这是存储过程代码。

Create procedure [dbo].[sp_enter_new_student] 
    @Prenom_detudiant [varchar](50)  = NULL, 
    @nom_detudiant [varchar](50)  = NULL,
    @nationalite_detudiant [varchar](40)  = NULL,
    @langue_parle [varchar](15)  = NULL,
    @nombre_denfants [int] = NULL,
    @sexe_detudiant [char](1) = NULL,
    @program_pk [int] = NULL,
    @Numero_detudiant int = NULL
As 
Declare @numOfRowsBefore int = 0; 
declare @numOfRowsAfter int = 0; 
declare @Error int = -100; 
BEGIN  
    SET NOCOUNT ON; 
    select @numOfRowsBefore = COUNT(*) from tbl_students where Numero_detudiant = @Numero_detudiant; 

    if (@numOfRowsBefore > 0)
    begin
        Print "Student already exists, Insertion will fail!";
        Print "Insertion Failure ! " + CONVERT(varchar(10), @numOfRowsBefore);
        return @numOfRowsBefore; -->  -1 indicates insertion failure
    End
    else if (@numOfRowsBefore = 0)
    begin
        Print "Student doesn't exists, Insertion will be Success!";
        Print "Insertion Success ! " + CONVERT(varchar(10), @numOfRowsBefore);
        return @numOfRowsBefore; -->  -1 indicates insertion failure
    End

END 

这是一个 C# 代码


public int enregistreNouveauEtudiant(string Prenom_detudiant, string nom_detudiant, string nationalite_detudiant, string langue_parle, string nombre_denfants, string sexe_detudiant, string program_pk, string Numero_detudiant)
{
    int numberOfRowsInserted = 0;
    //try
    //{ 

    SqlConnection connection = this.GetConnection();
    SqlCommand insertStudentCommand = new SqlCommand("sp_enter_new_student", connection);
    connection.Open();
    insertStudentCommand.CommandType = CommandType.StoredProcedure;

    //insertStudentCommand.Parameters.Add("@ID", SqlDbType.Int);
    //insertStudentCommand.Parameters["@ID"].Direction = ParameterDirection.Output;

    insertStudentCommand.Parameters.Add("@Prenom_detudiant", SqlDbType.VarChar, 50).Value = Prenom_detudiant;
    insertStudentCommand.Parameters.Add("@nom_detudiant", SqlDbType.VarChar, 50).Value = nom_detudiant;
    insertStudentCommand.Parameters.Add("@nationalite_detudiant", SqlDbType.VarChar, 40).Value = nationalite_detudiant;
    insertStudentCommand.Parameters.Add("@langue_parle", SqlDbType.VarChar, 15).Value = langue_parle;
    insertStudentCommand.Parameters.Add("@nombre_denfants", SqlDbType.Int).Value = nombre_denfants;
    insertStudentCommand.Parameters.Add("@sexe_detudiant", SqlDbType.Char, 1).Value = sexe_detudiant;
    insertStudentCommand.Parameters.Add("@program_pk", SqlDbType.Int).Value = program_pk;
    insertStudentCommand.Parameters.Add("@Numero_detudiant", SqlDbType.Int).Value = Numero_detudiant;

    numberOfRowsInserted = insertStudentCommand.ExecuteNonQuery();

    connection.Close();

    return numberOfRowsInserted;
}

有人可以看看这个问题吗?我也使用了 try 和 catch 块,在这种情况下它绝对没用。

谢谢。

【问题讨论】:

    标签: c# stored-procedures sql-server-2008-r2


    【解决方案1】:

    您应该在 c# 代码中使用 ExecuteScalar 而不是 ExecuteNonQuery

    并在您的 sql 代码中使用 SELECT 而不是 return

    【讨论】:

    • 嗨 GregM,我更改了这一行 numberOfRowsInserted = insertStudentCommand.ExecuteNonQuery(); to numberOfRowsInserted = Convert.ToInt16 (insertStudentCommand.ExecuteScalar());现在,无论数据库中是否存在记录,我都得到 0。
    • Oups,更改选择而不是返回,我已经编辑了我的答案
    • 谢谢,GregM,我将返回更改为 Select。现在可以使用了 :),干杯!
    • 欢迎您,只是想知道您为什么接受其他答案?
    • 大声笑,我没有注意到我们必须接受一次回答。我实现了你的建议,并尝试测试 RBaryYoung 的建议,拥有多个解决方案总是好的;)。
    【解决方案2】:

    来自 Microsoft 文档 (here):

    对于 UPDATE、INSERT 和 DELETE 语句,返回值为 受命令影响的行数。当触发器存在于 正在插入或更新的表,返回值包括数字 受插入或更新操作和数量影响的行数 受触发器或触发器影响的行数。对于所有其他类型的 语句,返回值为-1。如果发生回滚,则返回 值也是-1。

    请注意,由于您的命令 ("sp_enter_new_student") 是一个隐式 EXECUTE 命令,它既不是 UPDATE、INSERT 也不是 DELETE,因此将始终返回 -1。

    如果要向程序返回单个值,一种方法是使用ExecuteScalar,然后在存储过程中返回带有SELECT @numOfRowsBefore; 的值,而不是使用Return 语句。


    但是,如果你想保留使用 SQL Return 语句的方法,那么你可以通过在你的 c# 代码中添加这样的行来做到这一点:

    SqlParameter retval = insertStudentCommand.Parameters.Add("retval");
    retval.Direction = ParameterDirection.ReturnValue;
    
    insertStudentCommand.ExecuteNonQuery();
    numberOfRowsInserted = (int)insertStudentCommand.Parameters["retval"].Value;
    

    (我认为这可行,但我现在无法测试)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-18
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多