Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL/存储过程问题_C#_Sql_Stored Procedures - Fatal编程技术网

C# SQL/存储过程问题

C# SQL/存储过程问题,c#,sql,stored-procedures,C#,Sql,Stored Procedures,有一种情况,我正在执行一个存储过程,并在返回55条记录的c#代码中获取结果,现在我在行中有一列,需要显示为执行另一个sql查询的结果。我尝试了所有的方法,但不知道如何实现。请帮忙 我称之为运行良好的过程的C代码是: public static MonthlyFinancialReportCollection GetList(SASTransaction withinTransaction) { MonthlyFinancialReportCollection records = null

有一种情况,我正在执行一个存储过程,并在返回55条记录的c#代码中获取结果,现在我在行中有一列,需要显示为执行另一个sql查询的结果。我尝试了所有的方法,但不知道如何实现。请帮忙

我称之为运行良好的过程的C代码是:

public static MonthlyFinancialReportCollection GetList(SASTransaction withinTransaction)
{
    MonthlyFinancialReportCollection records = null;

    using (DataSet ds = Helpers.GetDataSet("ShowPremiumCalcReport", withinTransaction))
    {
        if (ds.Tables.Count > 0)
        {
            records = new MonthlyFinancialReportCollection();

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (!string.IsNullOrEmpty(dr[0].ToString())) 
                     records.Add(FillDataRecord(dr));                        
            }
        }
    }

    return records;
}
现在我想对上述函数中调用的过程运行一个查询,这样它将用查询的计算结果填充列中的一个

select 
    Sum(WorkingHours.WHO_Amount * dbo.PremiumLevel.PLE_Premium) as Snr_Teaching_Amt
from policy, policyline, coveroption,
     premiumlevel, WorkingHours
where policy.pol_id=policyline.pli_pol_id 
and   policyline.pli_cop_seniorteachingcoverid=coveroption.cop_id 
and   premiumlevel.ple_own_id=policy.pol_own_id 
and   premiumlevel.ple_sca_id=coveroption.cop_sca_id 
and   WorkingHours.who_pos_id=policyline.pli_pos_id
and   pol_dcsf=row["snr teaching staff"]`
这不是问题

第一选项

 You execute the first stored procedure.
 Then foreach row 
    You execute the Second one with a parameter from the first one
 endfor
第二选项:

 You use a Cursor on the first stored procedure to get all the results in one call

最慢、最简单的方法:

  • 使您的第二个查询成为一个接受“snr教学人员”的存储过程 作为一个参数
  • 使用以下命令从第一个查询向DataTable添加新列: 表[0].Columns.Add(“Snr\u Teaching\u Amt”,typeof(decimal))
  • 为创建的第二个存储过程创建SqlCommand和SqlParameter
  • 循环数据表并将“snr教学人员”放入SqlParameter的值中
  • 使用ExecuteScalar运行SqlCommand,返回的值是第二次查询的结果,可以保存在创建的列中
但是我不推荐这种方式,因为您将有55+1服务器往返,这并不酷

中间路线:

因为第二个查询返回一个值,所以将第二个查询设为Sql函数。您可以在第一个过程中调用它,如:

SELECT *, dbo.MyFunction([snr teaching staff]) AS Snr_Teaching_Amt FROM ...
这将具有更好的性能,并且更易于维护。
但是,我同意Massanu的观点,您可以使用子查询组合这些过程。祝你好运;-)

您将调用另一个存储过程55次?你不能只用一个存储过程来获取所有数据吗?不,我不能,因为第一组数据是用不同的条件生成的,并且是一个透视查询,我尝试在第一个存储过程中注入查询,但失败了。这不是要将存储过程运行到另一个存储过程中,即第一个存储过程生成的数据,我想运行第二个存储过程,它将在第一个结果集的一列中填充结果数据Acomb小学(2000)295.20空1644.75空Applefields学校(7032)空12255.00空约克行政长官初级学校大主教(3229)410.40空獾山小学(2431)159.50 833.75 93.50空主教索普幼儿学校(2386)605.00空空现在在最后一列,我想显示第二次查询的结果。因此,您可以将第二个选项与游标一起使用。但是您不能在存储过程中调用存储过程,因此您必须合并到一个唯一的存储过程中。我认为,这是一个不错的选择,如果出现问题,我将尝试返回。谢谢大家。我试着这样做,然后到达某个地方,你能检查一下,它是否有任何问题吗<代码>使用(DataSet ds=Helpers.GetDataSet(“ShowPremiumCalReport”){foreach(ds.Tables[0].Rows中的DataRow dr){using(DataSet ds1=Helpers.GetDataSet(“ShowSnrTechCalc”,withinTransaction)){foreach(ds1.Tables[0].Rows中的DataRow dr1){records.Add(FillDataRecord(dr,dr1));}}我认为您正在做一些额外的工作:)ShowSnrTeachingCalc只返回一个值,因此您不需要创建或循环ds1。要添加列:ds.Tables[0]。Columns.add(“SnrTeachingCalc”,typeof(decimal));然后在第一个循环中:dr[“SnrTeachingCalc”]=GetSnrTeachingCalc(dr[“snr teaching staff”]);GetSNrTechGCalc是一种方法,它只调用您创建的第二个sp。查找MSDN上的ExecuteScalar以获取示例。