通过存储过程将值返回到c#

时间:2014-07-13 13:40:08

标签: c# sql sql-server

我想将少量列和行(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 个答案:

答案 0 :(得分: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();

答案 1 :(得分: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();