调用没有任何参数的存储过程c#

时间:2014-02-28 13:35:43

标签: c# asp.net tsql stored-procedures

我是T-SQL中存储过程的新手,我试图调用我的存储过程来处理我的asp页面。我不知道如何解决这个问题我只是为我的Select语句创建一个SQL字符串,或者我是否必须包含所有声明变量。我在下面发布了我的声明和选择声明。谢谢你的帮助。

DECLARE @CasesPerHour decimal(18,2);

Declarations
DECLARE @Line1Start datetime;
DECLARE @Line1Breaks decimal(10,2);
DECLARE @Line1ScaleCount int;
DECLARE @Line1Scale1Percentage varchar(10);
DECLARE @Line1Scale2Percentage varchar(10);
DECLARE @Line1Scale3Percentage varchar(10);
DECLARE @Line1Scale4Percentage varchar(10);
DECLARE @Line1Scale5Percentage varchar(10);
DECLARE @Line1Scale6Percentage varchar(10);
DECLARE @Line1Scale7Percentage varchar(10);
DECLARE @Line1Percentage  varchar(10);
DECLARE @Line1TargetCaseCount decimal(10,2);


Set statement

SET @CasesPerHour = 70.00

--Line 1 Sets
SET @Line1Start = (SELECT TOP 1 PrintDateTime
FROM CompletedCaseManifest
WHERE LineNumber = 1 and PrintDateTime > (CASE WHEN (cast(getdate() as time(0)) < '06:00:00' OR cast(getdate() as time(0)) > '17:15:00') then (CASE WHEN cast(getdate() as time(0)) < '06:00:00' then DATEADD(day, DATEDIFF(day, 1, GETDATE()), '17:15:00') else DATEADD(day, DATEDIFF(day, 0, GETDATE()), '17:15:00') end) else DATEADD(day, DATEDIFF(day, 0, GETDATE()), '06:00:00') end))
SET @Line1Breaks = ISNULL((SELECT SUM(BreakLength) FROM BreakTracker WHERE LineNumber = 1 AND BreakStartTime > @Line1Start), 0)/60.00
SET @Line1ScaleCount = (SELECT COUNT(Distinct ScaleNumber) FROM CompletedCaseManifest Where LineNumber = 1 and PrintDateTime > @Line1Start)
SET @Line1TargetCaseCount = (((datepart(minute,convert(varchar(8),cast(getdate() - @Line1Start as time(0))))/60.00)
 + datepart(hour,convert(varchar(8),cast(getdate() - @Line1Start as time(0)))) - @Line1Breaks)*@CasesPerHour)
SET @Line1Scale1Percentage = Convert(decimal(10,2),((SELECT COUNT(CaseID) FROM CompletedCaseManifest WHERE LineNumber = 1 and ScaleNumber = 1 and CaseVoided = 0 and PrintDateTime > @Line1Start)/@Line1TargetCaseCount)*100)
SET @Line1Scale2Percentage = Convert(decimal(10,2),((SELECT COUNT(CaseID) FROM CompletedCaseManifest WHERE LineNumber = 1 and ScaleNumber = 2 and CaseVoided = 0 and PrintDateTime > @Line1Start)/@Line1TargetCaseCount)*100)
SET @Line1Scale3Percentage = Convert(decimal(10,2),((SELECT COUNT(CaseID) FROM CompletedCaseManifest WHERE LineNumber = 1 and ScaleNumber = 3 and CaseVoided = 0 and PrintDateTime > @Line1Start)/@Line1TargetCaseCount)*100)
SET @Line1Scale4Percentage = Convert(decimal(10,2),((SELECT COUNT(CaseID) FROM CompletedCaseManifest WHERE LineNumber = 1 and ScaleNumber = 4 and CaseVoided = 0 and PrintDateTime > @Line1Start)/@Line1TargetCaseCount)*100)
SET @Line1Scale5Percentage = Convert(decimal(10,2),((SELECT COUNT(CaseID) FROM CompletedCaseManifest WHERE LineNumber = 1 and ScaleNumber = 5 and CaseVoided = 0 and PrintDateTime > @Line1Start)/@Line1TargetCaseCount)*100)
SET @Line1Scale6Percentage = Convert(decimal(10,2),((SELECT COUNT(CaseID) FROM CompletedCaseManifest WHERE LineNumber = 1 and ScaleNumber = 6 and CaseVoided = 0 and PrintDateTime > @Line1Start)/@Line1TargetCaseCount)*100)
SET @Line1Scale7Percentage = Convert(decimal(10,2),((SELECT COUNT(CaseID) FROM CompletedCaseManifest WHERE LineNumber = 1 and ScaleNumber = 7 and CaseVoided = 0 and PrintDateTime > @Line1Start)/@Line1TargetCaseCount)*100)
SET @Line1Percentage = Convert(decimal(10,2),((SELECT COUNT(CaseID) FROM CompletedCaseManifest WHERE LineNumber = 1 and CaseVoided = 0 and PrintDateTime > @Line1Start)/(@Line1TargetCaseCount*@Line1ScaleCount))*100)

选择声明

Select @Line1Scale1Percentage + '%' as Line1Scale1, 
@Line1Scale2Percentage + '%' as Line1Scale2, 
@Line1Scale3Percentage + '%' as Line1Scale3, 
@Line1Scale4Percentage + '%' as Line1Scale4, 
@Line1Scale5Percentage + '%' as Line1Scale5, 
@Line1Scale6Percentage + '%' as Line1Scale6, 
@Line1Scale7Percentage + '%' as Line1Scale7, 
@Line1Percentage + '%' as Line1Average,

2 个答案:

答案 0 :(得分:1)

通常,调用存储过程只是使用SqlCommand命令类型执行StoredProcedure

using (SqlCommand cmd = new SqlCommand("storedProcedureName", connection))
{
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.ExecuteNonQuery();
}

您使用ExecuteNonQuery还是使用任何其他ExecuteXYZ方法取决于您是否预期来自呼叫的结果。如果存储过程返回结果集,例如,您可能希望使用ExecuteReader,如下所示:

using (SqlDataReader reader = cmd.ExecuteReader())
{
    while (reader.Read())
    {
        // Handle one item of the result set.
    }
}

在您的情况下,存储过程返回一条记录,因此您可能希望使用ExecuteReader

答案 1 :(得分:1)

使用EXEC语句( http://technet.microsoft.com/en-us/library/ms189915.aspx )调用存储过程。

在存储过程的主体内声明的变量不会传递给它。只有在存储过程主体之前的括号中指定的参数才会从外部传递给它。

关于存储过程的Microsoft文档( http://technet.microsoft.com/en-us/library/ms187926.aspx )可以引人注目,但如果您专注于查看底部的示例,可能会为您清理。