2014-07-13 23 views
0

我想向C#代碼返回少量列和行(Temp表)。這是我的存儲過程。通過存儲過程將值返回給c#

CREATE PROC [dbo].[GetInvoice]  
(    
@ClientId as INT , 
@MatterId as INT, 
@DateFrom datetime, 
@DateTo datetime 
)    
AS    
BEGIN    
    -- SET NOCOUNT ON added to prevent extra result sets from    
    -- interfering with SELECT statements.    
    SET NOCOUNT ON;    

    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED 

    select *,'Expense' as TblName 
    into #tempExpense 
    from 
     (select e.ClientId 
       , CAST(e.DateAdded AS DATE) as [Date] 
       , e.MatterId 
       , e.TotalAmount 
       , e.UserId 
       , e.Id As SourceRowID 
     from Expense e) AS E 

    select *, 'Times' as TblName 
    into #tempTimes 
    from 
     (select t.clientid 
       , CAST(t.[Date] AS DATE) AS [Date] 
       , t.MatterId 
       , t.TotalAmount 
       , t.UserId 
       , t.TimeID as SourceRowID 
     from Times t) AS T 

    select A.* 
    into #tempResult 
    from 
     (select * from #tempExpense 
     UNION ALL 
     select * from #tempTimes) AS A 

    SELECT DENSE_Rank() OVER (ORDER BY ClientID, Date, MatterID) As Rank,* 
    INTO #tempResult_Final 
    FROM #tempResult 

    UPDATE Expense 
    SET Expense_Rank = R.[Rank] 
    FROM Expense E 
    INNER JOIN #tempResult_Final R ON E.ID = R.SourceRowID AND TblName = 'Expense' 

    UPDATE Times 
    SET Times_Rank = R.[Rank] 
    FROM Times E 
    INNER JOIN #tempResult_Final R ON E.TimeID = R.SourceRowID AND TblName = 'Times' 

    select 
    tr.ClientId, m.MatterName, c.Name as ClientName, 
    tr.[Date] as DateInvoice, tr.MatterId, tr.UserId, 
    u.FirstName + ' ' + u.LastName as UserFullName, 
    Sum(tr.TotalAmount) as Total, 
    MAX(tr.Rank) AS Rank 
    from 
    #tempResult_Final tr 
    inner join 
    Matters m ON m.MatterID = tr.MatterId 
    inner join 
    Client c ON c.ClientId = tr.ClientId 
    inner join 
    [User] u ON u.UserId = tr.UserId 
    where 
     tr.ClientId = (Case when @ClientId = 0 then tr.ClientId else @ClientId end) 
     and tr.MatterId = (Case when @MatterId = 0 then tr.MatterId else @MatterId end) 
     and tr.[Date] between isnull(@DateFrom,cast('1900/01/01' as date)) 
     and isnull(@Dateto,cast('2999/01/01' as date)) 
    group by 
     tr.clientid, m.MatterName, c.Name, tr.[Date], tr.MatterId, 
     tr.UserId , u.FirstName, u.LastName 
    order by 
     [Date] 
END 
GO 

請幫忙。

+2

第二次打擊的谷歌搜索「通過存儲過程返回值到C#」的:http://stackoverflow.com/questions/6210027/c-sharp-calling-sql-server-stored-procedure-with-返回值 –

回答

0

使用參考guide和谷歌。此代碼可以幫助您的問題:

SqlConnection sqlConnection1 = new SqlConnection("Your Connection String"); 
SqlCommand cmd = new SqlCommand(); 
SqlDataReader reader; 

cmd.CommandText = "StoredProcedureName"; 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.Connection = sqlConnection1; 


//ADD parameters and return value 

    cmd.Parameters.AddWithValue("par1Name", "par1Value"); 

    var returnParameterVariable = cmd.Parameters.Add("@ReturnVal", SqlDbType.NVarChar); 
    returnParameterVariable .Direction = ParameterDirection.ReturnValue; 

sqlConnection1.Open(); 

reader = cmd.ExecuteReader(); 

sqlConnection1.Close(); 
0

How to: Execute a Stored Procedure that Returns Rows

下面的代碼添加到您要執行從代碼的方法。您通過調用該命令的ExecuteReader方法(例如,ExecuteReader)來返回行。數據在DataReader中返回。有關訪問DataReader中數據的更多信息,請參閱使用DataReader檢索數據。

SqlConnection sqlConnection1 = new SqlConnection("Your Connection String"); 
SqlCommand cmd = new SqlCommand(); 
SqlDataReader reader; 

cmd.CommandText = "GetInvoice"; 
cmd.CommandType = CommandType.StoredProcedure; 
cmd.Connection = sqlConnection1; 

sqlConnection1.Open(); 

reader = cmd.ExecuteReader(); 
// Data is accessible through the DataReader object here. 

sqlConnection1.Close(); 
+0

我可以在C#上接收值。但我的SP沒有返回任何價值。 –

+0

在執行我的SP @ sql後給出以下輸出,但不會在c#處返回任何值。 ClientID的物質名username發票日期matterID用戶ID總排行 \t物質..雀塔\t \t 2014年7月12日36 \t \t 18 42.00 \t 物467 \t雀塔.. \t 2014年7月12日\t \t \t 225.00 3 返回值 0 –