使用存储过程访问列表

时间:2013-06-12 21:38:03

标签: c# .net sql-server tsql

我有一个存储过程,我想用它来获取我的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();

1 个答案:

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