2013-06-12 74 views
0

我有存儲過程,我想用它來獲取我的c#類中的記錄列表。使用存儲過程訪問列表

Stored procedure is 
    ALTER Procedure [dbo].[testing] 
    as 
    Begin 
    DECLARE @p66 DateTime2 = '2013-04-4 00:00:00.0000000' 
    DECLARE @p1 Int = 9 
    SELECT [t0].[cDate] AS [CDate], [t0].[nDate] AS [NDate], [t0].[eNum] AS [ENumber], [t0].[sId] AS [SId], [t0].[sId] AS [SId] 
    FROM [t_elist] AS [t0] 
    WHERE ([t0].[neDate] = @p0) AND ([t0].[eNum] <= @p99) 

如何調用此存儲過程將我的C#代碼中所有記錄的列表返回給我?

After creating connection, sql command etc. Should I? 
     cmd.parameter.Add(storedprocName); 
     var list=storedprocName(); 

回答

0

請嘗試以下操作。

using (var conn = new SqlConnection("you connection string here")) 
     using (var command = new SqlCommand("[dbo].[testing] ", conn) 
     { 
      CommandType = CommandType.StoredProcedure 
     }) 
     { 
      SqlDataAdapter adapt = new SqlDataAdapter(command); 
      DataTable dt = new DataTable(); 
      adapt.Fill(dt); 

      foreach (DataRow x in dt.Rows) { 
       Console.WriteLine("First column value : " + x[0]); 
      } 
     }