【问题标题】:Subsonic 3 - how do you execute a stored procedure that returns a value?Subsonic 3 - 如何执行返回值的存储过程?
【发布时间】:2009-07-28 17:56:48
【问题描述】:

如何在 subsonic 3.0 中执行一个返回值的存储过程?例如,我的许多存储过程都返回@@identity,而我不知道如何在不重新查询表的情况下访问该值。同样,不是输出参数,而是返回值。

【问题讨论】:

    标签: subsonic subsonic3


    【解决方案1】:
    StoredProcedure sproc = db.FancySchmancySproc();
    sproc.Execute();
    int output = (int)sproc.Output; // returns type 'object' so you'll need to cast
    

    【讨论】:

      【解决方案2】:

      这对我有用:

      StoredProcedure sproc = db.FancySchmancySproc();
      sproc.ExecuteScalar<int>();
      

      【讨论】:

        【解决方案3】:
        StoredProcedure sp = SPs.TestProcedure(0);
        
        sp.Command.AddReturnParameter();
        
        object retValue = sp.Command.Parameters.Find(delegate(QueryParameter p)
        { 
          return p.Mode == ParameterDirection.ReturnValue; 
        }).ParameterValue;
        
        if (retValue!=null)
        {
        
          // cast the value to the type expected
        
          int rc = (int) retValue;
        
        }
        

        【讨论】:

          【解决方案4】:

          好吧......我已经按照 John Sheehan 所说的进行了操作,但突然间它开始给我一个例外。

          我只是在用下面的 SP 进行测试。


          设置 ANSI_NULLS ON 去 设置 QUOTED_IDENTIFIER ON 去 创建程序TestLocal

          @a 数字(18,0) 作为

          开始

          设置无计数;

          返回 10

          结束


          我的 C# 代码如下


          StoredProcedure sp = new DB().TestLocal(1);

          sp.Execute();

          int timeLeft = (int)sp.Output;

          返回时间;


          编辑 1:

          我通过执行以下操作使其正常工作,但我认为这不是正确的方法。


          StoredProcedure sp = new DB().TestLocal(1);

          sp.Command.AddReturnParameter();

          sp.Execute();

          int myReturnValue = (int)sp.Command.OutputValues[0];

          返回我的返回值;

          【讨论】:

            【解决方案5】:

            我一直在像这样使用 ActiveRecord 运行存储过程;

            // Connect to DB and run the stored proc into a dataset
            myDatabasedb db = new myDatabasedb();
            DataSet ds = db.myStoredProc(firstparam, secondparam, etc);
            
            // Now you can do what you like with the dataset!
            Gridview1.datasource = ds.executeDataSet();
            

            【讨论】:

              【解决方案6】:

              这是我的想法,但 sproc.output 始终为空。不知道最新版本3.0.0.3有没有bug

              【讨论】:

                【解决方案7】:
                StoredProcedure sproc = db.FancySchmancySproc();
                sproc.ExecuteDataSet();
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2011-03-15
                  • 2019-02-16
                  • 2011-10-20
                  • 1970-01-01
                  • 1970-01-01
                  • 2012-07-24
                  • 1970-01-01
                  相关资源
                  最近更新 更多