存储过程don';不要再开火了

本文关键字:开火 don 存储过程 | 更新日期: 2023-09-27 18:25:18

我有一个存储过程SetMonthlyGroup(代码如下所示),但有一个大但不复杂的过程,它只是在每个月为每个@Accounts集合@Amount重复,为所有@monthCount 重复

顺便说一下,我测试了一下,它确实有效。

ALTER PROCEDURE [yeganeh].[SetMonthlyGroup] 
    @Accounts text,
    @StartDate datetime,
    @monthCount int,
    @SanNum int,
    @Amount bigint 
AS
BEGIN
    DECLARE @returnList AS TABLE (Name nvarchar(500),
                                  IDSource int,
                                  id int NOT NULL PRIMARY KEY IDENTITY(1,1)
                                 )
 
    DECLARE @result int
    SET @result = (SELECT COUNT(*) 
                   FROM dbo.splitstring(@Accounts,','))
    INSERT INTO @returnList  
        SELECT *  
        FROM dbo.splitstring(@Accounts,',')
    SET @result = (SELECT COUNT(*) FROM @returnList)
 
    DECLARE @i int
    SET @i = 0
    DECLARE @AccountIndex nvarchar(20)
    DECLARE @monthIndex int
    SET @monthIndex = 0
    DECLARE @payDate datetime
    SET @payDate = getdate()
 
    begin try
    begin transaction
        while @i < @resualt
        begin
            set @i = @i + 1
            select @AccountIndex = Name 
            from @returnList 
            where id = @i
            set @monthIndex = 0
            while @monthIndex < @monthCount
            begin
                set @payDate = (DATEADD(month, @monthIndex, @StartDate))
            
                insert into Sandogh_Monthly 
                values (@SanNum, @AccountIndex, @Amount, 'NotPaid', @payDate) 
                set @monthIndex = @monthIndex + 1
            end
         end
     
         insert into Logs 
         values (@SanNum, 'Monthly', 'System Log', getdate(), 'Transacction Commit', NULL)
         commit
     end try
     begin catch
         rollback
         insert into Logs 
         values (@SanNum, 'Monthly', 'System Log', getdate(), 'Transacction rollback', NULL)
     end catch
END

我将此存储过程作为事务执行,并将日志保存在日志表中

有时我使用ADO.net执行存储过程,ExecuteNonQuery返回true,但日志表中没有显示行,月表中也没有显示行。

我这样称呼它:

public bool inserttMonthly(string accounts, int month, DateTime startDate)
{
        DB dbsql = new DB();
        dbsql.AddParams("@Accounts", accounts);
        dbsql.AddParams("@StartDate", startDate);
        dbsql.AddParams("@monthCount", month);
        dbsql.AddParams("@SanNum", this.Mont_SanNum);
        dbsql.AddParams("@Amount", this.Mont_Amount);
        return dbsql.ExecuteWithNoResultDSParamsSP("SetMonthlyGroup");
    }

和db级

public bool ExecuteWithNoResultDSParamsSP(string storedProcedureName)
{
    this.Connect();
    try
    {
        this.cmd.CommandText = storedProcedureName;
        this.cmd.CommandType = CommandType.StoredProcedure;
        this.cmd.Connection = this.cn;
        this.cmd.BeginExecuteNonQuery();
        this.cn.Close();
        return true;
    }
    catch (Exception ex)
    {
        return false;
    }
}

我像下面的代码一样更改db类,它返回:-

值Id=1,状态=RanToCompletion,方法=";{null}";,结果="System.IAsyncResult{System.Threading.Tasks.Task}

 System.IAsyncResult value= this.cmd.BeginExecuteNonQuery();
            string msg = value.ToString();

最后我决定用Ajax来做。用存储过程竞争新方法

function setMonth(account ) {
    // $("#Loading").show();
    setTimeout(function () {
        $.ajax({
            url: "../UCSandogh/Ajax/Month.aspx",
            type: "get",
            dataType: "HTML",
            async: false,
            cache: false,
            timeout: 30000,
            data: { action: "setMonth", account: account, date: sDate, amount: moneyAmount, monthCount: monthCountInput },
            error: function () {
                return false;
            },
            success: function (msg) {
                progressBar(parseInt(currentProg));
                if (msg) {
                    lastInsertStatus = true;
                    return true;
                } else {
                    lastInsertStatus = false;
                    return false;
                }
            }
        }).done(NextAcc())
    }, 3000);
}

存储过程don';不要再开火了

最后我决定用Ajax来完成。用存储过程竞争新方法

function setMonth(account ) {
    // $("#Loading").show();
    setTimeout(function () {
        $.ajax({
            url: "../UCSandogh/Ajax/Month.aspx",
            type: "get",
            dataType: "HTML",
            async: false,
            cache: false,
            timeout: 30000,
            data: { action: "setMonth", account: account, date: sDate, amount: moneyAmount, monthCount: monthCountInput },
            error: function () {
                return false;
            },
            success: function (msg) {
                progressBar(parseInt(currentProg));
                if (msg) {
                    lastInsertStatus = true;
                    return true;
                } else {
                    lastInsertStatus = false;
                    return false;
                }
            }
        }).done(NextAcc())
    }, 3000);
}