从存储过程的结果集填充的ArrayList [英] Populate ArrayList from Stored Procedure result set

查看:173
本文介绍了从存储过程的结果集填充的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储过程返回一个整数集。我需要帮助在C#中填充这些结果的ArrayList(VS 2005)Web应用程序。

I have a stored procedure that returns a set of integers. I need help populating an ArrayList of these results in a C# (VS 2005) web application.

我意识到这可能是一个非常简单的过程,但我没有能力给它,也没有词汇来研究它,显然。

I realize this is probably a very simple process, but I don't have the skills to it, nor the vocabulary to research it, apparently.

我的假设是这个数据表被正确填充:

My assumptions are this DataTable is being populated properly:

public static DataTable GetAllVendors()
{
     OleDbCommand cmd = Data.GetCommand(Configuration.DatabaseOwnerPrefix + ".GetAllInformationAndHelpVendorIds", Connections.MyDbConnection);
     return Data.RunCommand(cmd).Tables[0];
 }

我不知道是怎么得到的结果返回到一个ArrayList或者可与contains()方法来评估其它数据类型。

What I don't know is how to get the results back into an ArrayList or another datatype that may be evaluated with the Contains() method.

编辑:ArrayList是一个较旧的技术,我会采取不使用它的意见。谢谢你。

ArrayList is an older technology, and I'll take the advice of not using it. Thanks.

推荐答案

你应该做的是调用的ExecuteReader() CMD 的对象,像这样:

What you should do is invoke ExecuteReader() on that cmd object, like so:

public static IEnumerable<int> GetAllVendors()
{
    using (var cmd = Data.GetCommand(Configuration.DatabaseOwnerPrefix + ".GetAllInformationAndHelpVendorIds", Connections.MyDbConnection))
    {
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                yield return reader.GetInt32(0);
            }
        }
    }
}

这假设SQL将返回一个表,其第一列(通过索引 0 的方法 GetInt32等())将是你正在寻找的ID。如果SQL另一列返回的ID,只需调整索引你希望找到他们在列。

This assumes that the SQL will return a table whose first column (identified by the index 0 to the method GetInt32()) will be the ID you're looking for. If the SQL returns the IDs in another column, just adjust the index to the column you're expecting to find them in.

该解决方案还预计,该命令的连接已经打开。如果不是,你可以做 cmd.Connection.Open()前刚 cmd.ExecuteReader()

This solution also expects the command's connection to already be open. If it isn't, you can do cmd.Connection.Open() just before cmd.ExecuteReader().

这篇关于从存储过程的结果集填充的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆