我有这个存储过程:

ALTER PROCEDURE [dbo].[DeleteFromSchoolMain]
    @TblName VARCHAR(50),
    @MainID VARCHAR(10),
    @TblCol VARCHAR(50),
    @rowsCount INT OUTPUT
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @RelTbl AS Nvarchar(50)
    DECLARE @ColForFK AS Nvarchar(50)
    DECLARE @TblMaterial AS NVARCHAR(50)
    DECLARE @ColMaterialID AS NVARCHAR(50)
    DECLARE @ColMaterialFK AS NVARCHAR(50)
    DECLARE @cmdForMaterial AS NVARCHAR(max)
    DECLARE @TblSubject AS NVARCHAR(50)
    DECLARE @ColSubjectID AS NVARCHAR(50)
    DECLARE @ColSubjectFK AS NVARCHAR(50)
    DECLARE @cmdForSubject AS NVARCHAR(max)

    Set @ColForFK = SUBSTRING(@TblCol,1, DATALENGTH(@TblCol)-2)
    Set @ColForFK = @ColForFK+'FK'

    DECLARE @cmd AS NVARCHAR(max)
    DECLARE @TempCount int

    SET @MainID = ''''+@MainID+ ''''

    SET @RelTbl = 'ClassSubMatRelation'
    --For Class Material
    SET @TblMaterial = 'ClassMaterial'
    SET @ColMaterialID = 'ClassMaterialID'
    SET @ColMaterialFK = 'ClassMaterialFK'

    SET @cmd = N'UPDATE ' + @RelTbl + ' SET Status_Info = 0 WHERE ' +  @ColForFK + ' = ' + @MainID -- Delete from ClassRelation Table
    EXEC(@cmd)
    SET @TempCount = @@ROWCOUNT
    SET @rowsCount = @TempCount + @rowsCount;

    SET @cmd = N'UPDATE ' + @TblName + ' SET Status_Info = 0 WHERE ' +  @TblCol + ' = ' + @MainID -- Delete from Main Table
    EXEC(@cmd)
    SET @TempCount = @@ROWCOUNT
    SET @rowsCount = @TempCount + @rowsCount;

    -------------------------------------Board-----------------------------------
    IF (@TblName = 'Board')
    BEGIN

        SET @cmdForMaterial = N'UPDATE '+@TblMaterial+' Set Status_Info = 0 where '+@ColMaterialID+' in ( Select s.'+@ColMaterialFK+' from  '+@RelTbl+' s where s.' +  @ColForFK + ' = ' +  @MainID + ' AND s.ClassSubjectFK IN ( SELECT T.ClassSubjectFK FROM ' + @RelTbl + ' T WHERE  T.'+@ColForFK+' = '+ @MainID + ') and s.ClassSubjectFK NOT IN ( SELECT E.ClassSubjectFK FROM ' +@RelTbl+' E WHERE E.'+@ColForFK+' != '+ @MainID +') and s.'+@ColMaterialFK+'  is not null )'
        EXEC(@cmdForMaterial)
        SET @TempCount = @@ROWCOUNT
        SET @rowsCount = @TempCount + @rowsCount;
        SET @TempCount = 0;


        --For Class Subject
        SET @TblMaterial = 'ClassSubject'
        SET @ColMaterialID = 'ClassSubjectID'
        SET @ColMaterialFK = 'ClassSubjectFK'

        SET @cmdForMaterial = N'UPDATE '+@TblMaterial+' Set Status_Info = 0 where '+@ColMaterialID+' in ( Select s.'+@ColMaterialFK+' from  '+@RelTbl+' s where s.' +  @ColForFK + ' = ' +  @MainID + ' AND s.ClassSubjectFK IN ( SELECT T.ClassSubjectFK FROM ' + @RelTbl + ' T WHERE  T.'+@ColForFK+' = '+ @MainID + ') and s.ClassSubjectFK NOT IN ( SELECT E.ClassSubjectFK FROM ' +@RelTbl+' E WHERE E.'+@ColForFK+' != '+ @MainID +') and s.'+@ColMaterialFK+'  is not null )'
        EXEC(@cmdForMaterial)
        SET @TempCount = @@ROWCOUNT
        SET @rowsCount = @TempCount + @rowsCount;
        SET @TempCount = 0;


    END

        ------------------------------ Class Subject ------------------------------------
    ELSE IF (@TblName = 'ClassSubject')
    BEGIN

        SET @cmdForMaterial = N'UPDATE '+@TblMaterial+' Set '+@TblMaterial+'.Status_Info = 0 from '+@TblMaterial+' tm join '+@RelTbl+' rt on rt.'+@ColMaterialFK+' = tm.'+@ColMaterialID+' where rt.'+@ColForFK+ ' = '+ @MainID
        EXEC(@cmdForMaterial)

        SET @TempCount = @@ROWCOUNT
        SET @rowsCount = @TempCount + @rowsCount;
        SET @TempCount = 0;
    END

END
GO

我在 C# 中像这样执行它:
var rowsAffected = 0;

using (SqlConnection conn = new SqlConnection(Constants.Connection))
{
     conn.Open();

     SqlCommand cmd = new SqlCommand("DeleteFromSchoolMain", conn);
     cmd.CommandType = CommandType.StoredProcedure;

     cmd.Parameters.AddWithValue("@TblName", abundleBoard.TempName);
     cmd.Parameters.AddWithValue("@MainID", abundleBoard.MainID.ToString());
     cmd.Parameters.AddWithValue("@TblCol", abundleBoard.TblCol);

     SqlParameter outputParam = new SqlParameter();
     outputParam.ParameterName = "@rowsCount";
     outputParam.SqlDbType = System.Data.SqlDbType.Int;
     outputParam.Direction = System.Data.ParameterDirection.Output;
     cmd.Parameters.Add(outputParam);

     object o = cmd.ExecuteScalar();

     if (!outputParam.Value.Equals(DBNull.Value))
     {
         rowsAffected = Convert.ToInt32(outputParam.Value);
         string q = o.ToString();
     }

     conn.Close();
}

但我的问题是我没有在输出参数中得到任何东西。我不明白这个问题,我也觉得我的存储过程没有优化。我可以做些什么来使查询执行得更快?索引和分区我已经做到了最好的水平。

编辑:

所以在@SurjitSD 和@PeterB 的帮助下,我得到了一个解决方案:-

在我的程序中,我在顶部添加了一行:-
SET @rowsCount=0

在 C# 中,我更改了代码,例如:-
 cmd.ExecuteNonQuery();
 if (!cmd.Parameters["@rowsCount"].Value.Equals(DBNull.Value))
  {
   rowsAffected = Convert.ToInt32(cmd.Parameters["@rowsCount"].Value);
  }

现在这工作正常!

最佳答案

您正在 C# 中调用 ExecuteScalar
ExecuteScalar 将为您提供过程中 Select 语句返回的 firstRow 和 FirstColumn 值。

您需要使用 ExecuteNonQuery
ExecuteNonQuery 之后,您可以获得类似的值

cmd.Parameters["@rowsCount"].Value



使用 ExecuteScalar 的示例

Sql 程序
Alter procedure SomeProcedure
(
@someVariable int
:
:
)
Begin
   /*Some processing logic of procedure*/

    Select @SomeVariable //any variable Or Value from procedure needed as output in c#
End

C#
var result = cmd.ExecuteScalar();

———— 编辑 ——

在 c# 中没有获得值(value)的实际原因是(有解释)
@rowsCount 参数在使用前没有初始化。所以它是null 并且在执行 SET @rowsCount = @TempCount + @rowsCount 时,它​​实际上向 null 中添加了一些内容。并且向 null 添加任何内容都将是 null 作为结果。

所以 @rowsCount 总是 null 。在程序开始时设置 @rowsCount=0 有帮助。

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

10-12 13:55