[학습 노트] C# 저장 프로세스 전송 매개 변수를 실행하고 output 매개 변수와return 을 되돌려줍니다value 방법

1. 저장 프로세스
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		fangpan
-- Create date: 2012-2-1
-- Description:	<Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[ECSS30_Stat]
@BeginTime DATETIME,
@EndTime DATETIME,
@Count int OUTPUT
AS
BEGIN
	SET NOCOUNT ON;
	
	--1.     
	IF OBJECT_ID('TEMPDB..#TEMP_FANGPAN')IS NOT NULL
	DROP TABLE #TEMP_FANGPAN
	CREATE TABLE #TEMP_FANGPAN(
	[UserContent] [varchar](256),
	[RecTime] [datetime],
	[RecResult] [char](1),
	[RecResultCode] [varchar](32),
	[RecResultDesc] [varchar](256),
	[SubRecType] [varchar](16),
	[RecBizName] [varchar](32),
	[RecOperationName] [varchar](16)
	)
	
	--2.             
	INSERT INTO #TEMP_FANGPAN(
	[UserContent],
	[RecTime],
	[RecResult],
	[RecResultCode],
	[RecResultDesc],
	[SubRecType],
	[RecBizName],
	[RecOperationName]
	)
	select a.[UserContent],
	a.[RecTime],
	a.[RecResult],
	a.[RecResultCode],
	a.[RecResultDesc],
	a.[SubRecType],
	a.[RecBizName],
	a.[RecOperationName]
	from dbo.LogRec_Route a
	where [RecResult]='0'

	--3.  
	select @Count=COUNT(*) FROM #TEMP_FANGPAN
	where [RecTime]>=@BeginTime
	and [RecTime]<=@EndTime
	and [RecResultCode]='RouteResult_Items_IsNull'
	IF @Count!=0
	RETURN 1
	ELSE
	RETURN 0
END

2. 스토리지 프로세스 실행
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
namespace READDATA
{
    class Program
    {
        static void Main(string[] args)
        {
            SqlConnection conn = new SqlConnection("User ID=sa;Initial Catalog=ECSS20_logtest_AHMobile;Data Source=192.168.85.76;Password=ctitest");
            conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = conn;
            cmd.CommandText = "ECSS30_Stat";
            cmd.CommandType = CommandType.StoredProcedure;
            IDataParameter[] parameters={
                new SqlParameter("@BeginTime",SqlDbType.DateTime),
                new SqlParameter("@EndTime",SqlDbType.DateTime),
                new SqlParameter("@Count",SqlDbType.Int),
                new SqlParameter("return_value",SqlDbType.Int)
            };
            parameters[0].Value = "2011-01-01";
            parameters[1].Value = "2012-02-01";
            parameters[2].Direction = ParameterDirection.Output;
            parameters[3].Direction = ParameterDirection.ReturnValue;
            cmd.Parameters.AddRange(parameters);
            cmd.ExecuteNonQuery();
            conn.Close();
            Console.WriteLine("@Count:" + parameters[2].Value);
            Console.WriteLine("return_value:" + parameters[3].Value);
            Console.ReadKey();
        }
    }
}

좋은 웹페이지 즐겨찾기