存储过程不会激活一段时间

时间:2015-12-02 15:16:13

标签: c# sql-server stored-procedures ado.net sqltransaction

我有一个存储过程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 class

public bool ExecuteWithNoResultDSParamsSP(string storeProcedureName)
{
    this.Connect();
    try
    {
        this.cmd.CommandText = storeProcedureName;
        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,Status = RanToCompletion,Method =“{null}”,Result =“”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);
}

1 个答案:

答案 0 :(得分:0)

最后我决定通过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);
}