存储过程不返回值 - C#

时间:2018-02-24 11:01:40

标签: c# sql asp.net sql-server

我有这个存储过程:

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);
  }

现在这个工作正常!

1 个答案:

答案 0 :(得分:2)

您正在c#中调用ExecuteScalar

ExecuteScalar将为您提供程序中Select语句返回的firstRow和FirstColumn值。

您需要使用ExecuteNonQuery

ExecuteNonQuery之后,你可以得到像

这样的价值
cmd.Parameters["@rowsCount"].Value
  

如果您在ExecuteScalar后使用Select @rowsCount,也可以使用set @rowsCount获取价值。然后你不需要在sql和c#

中提到output direction任何参数

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#中没有获得价值的实际原因是(有解释)

在使用之前,参数@rowsCount未初始化。就是这样 null并且在执行SET @rowsCount = @TempCount + @rowsCount时,它实际上向null添加了一些内容。向null添加任何内容都将为null

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