C# 存储过程重复返回-1

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

我在 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 block ,在这种情况下它绝对没用。

谢谢。

最佳答案

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

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

关于C# 存储过程重复返回-1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13183806/

相关文章:

java - 执行了 CallableStatement,但未返回任何内容

sql - SQL Server 多行计算

c++ - 通过 C++ 中的 SOCI/ODBC 的 SQL Server 存储过程

mysql - 如何在MySQL "EXECUTE STATEMENT USING"语句中使用变量列表?

sql-server - C# 和 SQL Server 对于字符串是否有效 Base64 存在分歧 - 哪个是正确的?

sql-server - SQL Server : Could not find type in the assembly

c# - 使用扩展方法的简单本地化

c# - 正则表达式 - 检查字符串是否以零 + 空格或单独的零开头

c# - 对齐按钮背景图片

c# - 通过 C# 打开 VB 程序时日期格式不正确