我在同一个存储过程中有两个选择,例如
SELECT
FORMAT(AVG([DC].[ContractedAmount]) , '$###,###,###,###.##') AS [AverageContractedAmount],
FORMAT(MAX([DC].[ContractedAmount]) , '$###,###,###,###.##') AS [MaxContractedAmount],
FORMAT(MIN([DC].[ContractedAmount]) , '$###,###,###,###.##') AS [MinContractedAmount],
COUNT([DC].[ContractedAmount]) AS [TotalProjects]
FROM
[DesignCustomer] AS [DC]
INNER JOIN
[Design] AS [D] ON [DC].[DesignKey] = [D].[DesignKey]
INNER JOIN
[Task] AS [T] ON [D].[DesignKey] = [t].[DesignKey]
INNER JOIN
[ProjectDesign] AS [PD] ON [D].[DesignKey] = [PD].[DesignKey]
INNER JOIN
[Project] AS [P] ON [PD].[ProjectKey] = [P].[ProjectKey]
INNER JOIN
[Address] AS [A] ON [A].[AddressGuid] = [P].[ProjectGuid]
WHERE ...
SELECT
CONCAT([P].[LegacyKey] , ' ' , [P].[Name]) AS [Project],
CONCAT([D].[Name] , ' ' , [DT].[Abbreviation] , ' ' , [D].[DesignNumber]) AS [Design],
FORMAT([DC].[ContractedAmount] , '$###,###,###,###.##') AS [Amount],
[P].[NumberOfUnits] AS [Units]
FROM
[DesignCustomer] AS [DC]
INNER JOIN
[Design] AS [D] ON [DC].[DesignKey] = [D].[DesignKey]
INNER JOIN
[DesignType] AS [DT] ON [D].[DesignTypeGuid] = [DT].[DesignTypeGuid]
INNER JOIN
[Task] AS [T] ON [D].[DesignKey] = [t].[DesignKey]
INNER JOIN
[ProjectDesign] AS [PD] ON [D].[DesignKey] = [PD].[DesignKey]
INNER JOIN
[Project] AS [P] ON [PD].[ProjectKey] = [P].[ProjectKey]
INNER JOIN
[Address] AS [A] ON [A].[AddressGuid] = [P].[ProjectGuid]
WHERE ...
我将C#插入Datatable的方式是:
public DataTable ExeSQLEstimate(string sprocName, parameters... )
{
SqlCommand cmd = new SqlCommand(sprocName, this.dbconn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@MinWoodStories", SqlDbType.Int).Value = minWoodStories;
//etc
DataTable tbl = new DataTable("Table1")
{
Locale = System.Globalization.CultureInfo.InvariantCulture
};
SqlDataAdapter da = new SqlDataAdapter(cmd);
try
{
da.Fill(tbl);
}
catch (SqlException e)
{
this.HandleSQLError(e, "GetTableBySQL", sprocName);
}
finally
{
cmd.Dispose();
}
return tbl;
}
但是由于某种原因,我只能得到一行(第一次选择),而不是两行(每次选择一个)
为什么只返回一行?问候
答案 0 :(得分:2)
您的过程返回多个结果集,每个结果集将存储在单独的DataTable中。您要填充数据集而不是数据表。然后,您的数据集将具有2个数据表,其中包含过程的结果,您可以索引到DataSet.DataTables
属性中以访问数据。
这是一个简单的示例,p_MultipleResultsProc
返回2个结果集,如您的过程一样:
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString))
using (SqlCommand cmd = new SqlCommand("EXEC p_MultipleResultsProc", conn))
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(ds);
}
执行后,ds
将包含2个数据表,每个数据表包含过程调用中不同的结果集。