从SQL存储过程结果</int>填充的C#List <int>

时间:2011-08-30 18:35:43

标签: c# sql-server list stored-procedures

我在VS 2005(.NET 2.0)中使用C#,在上世纪中期使用的旧版CMS上使用SQL Studio 2005。我的任务是创建一个新的权限门,只允许某些用户查看该站点的某些部分。

根据我在发布此问题时收到的反馈,我需要帮助填写列表清单:Populate ArrayList from Stored Procedure result set

那么,现在,如何将存储过程中的值转换为List?我意识到这是一个新手问题,但我是新手...

非常感谢任何帮助。

6 个答案:

答案 0 :(得分:2)

假设您从DataReader获得结果,您只需读取每一行以将值添加到列表中。

List<int> ReadList(IDataReader reader)
{
    List<int> list = new List<int>();
    int column = reader.GetOrdinal("MyColumn");

    while (reader.Read())
    {
        list.Add(reader.GetInt32(column));
    }

    return list;
}

请记住在完成后再处理DataReader

答案 1 :(得分:2)

您可以尝试使用位于此MSDN page下的模型,使用带有SqlCommand和存储过程的参数。示例如下所示:

static void GetSalesByCategory(string connectionString, string categoryName)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        // Create the command and set its properties.
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandText = "SalesByCategory"; //Stored Procedure Name
        command.CommandType = CommandType.StoredProcedure;

        // Add the input parameter and set its properties.
        SqlParameter parameter = new SqlParameter();
        parameter.ParameterName = "@CategoryName";
        parameter.SqlDbType = SqlDbType.NVarChar;
        parameter.Direction = ParameterDirection.Input;
        parameter.Value = categoryName;

        // Add the parameter to the Parameters collection. 
        command.Parameters.Add(parameter);

        // Open the connection and execute the reader.
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();

        if (reader.HasRows)
        {
            while (reader.Read())
            {
                //Instead of displaying to console this is where you would add
                // the current item to your list
                Console.WriteLine("{0}: {1:C}", reader[0], reader[1]);
            }
        }
        else
        {
            Console.WriteLine("No rows found.");
        }
        reader.Close();
    }
}

答案 2 :(得分:1)

这取决于您如何检索结果 读者? 数据集? 别的什么?

使用

查看结果
foreach (int item in object...) {
List.Add(item);
}

或者可能(我不记得我脑子里的确切DataRow语法......)

foreach (datarow row in object.table[0].rows) {
List.Add(row[0]);
}

答案 3 :(得分:1)

IList<int> myInts = new List<int>();

using (IDbConnection connection = new SqlConnection("yourConnectionStringGoesHere"))
{
    using (IDbCommand command = new SqlCommand("spName", connection))
    {
        command.CommandType = CommandType.StoredProcedure;

        //command.Parameters.Add(...) if you need to add any parameters to the SP.
        connection.Open();

        using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
        {
            myInts.Add(Int32.Parse(reader["someIntField"].ToString()));
        }
    }
}

答案 4 :(得分:0)

由于您已经拥有该表,因此我们的想法是迭代该表,同时将供应商的ID添加到列表中。

List<VendorID_Data_Type> myList = new List<VendorID_Data_Type>();
foreach(DataRow r in GetAllVendors().Rows)
{
    myList.Add(r["VendorID"]);
}

答案 5 :(得分:0)

我最终做的是使用DataTable作为中间数据类型,由存储过程填充。然后,重构DataTable作为foreach循环中的数据源,我填充了List。我需要打开第二个问题来得出这个结论:2-Column DataTable to List<int> .NET 2.0