从存储过程结果集中填充 ArrayList

发布于 2024-12-02 04:06:52 字数 519 浏览 1 评论 0原文

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

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

我的假设是这个 DataTable 已正确填充:

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

我不知道如何将结果返回到 ArrayList 或可以使用 Contains() 方法评估的其他数据类型中。

编辑:ArrayList 是一项较旧的技术,我会采纳不使用它的建议。谢谢。

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];
 }

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

土豪 2024-12-09 04:06:53

根据您要排除的列表的百分比、随之而来的其他数据量、数据库的负载情况以及网络的速度,如果您在源头进行过滤,通常效果会更好拉下一堆数据只是为了丢弃它。

在 SQL Server 2008 中,您可以采取相反的方式 - 将列表传递到 表值参数,并在服务器上进行过滤。

在 SQL Server 2005 中,您可以使用效率稍低的方法来模拟同样的事情 - 将逗号分隔的值列表传递到存储过程中,然后存储过程解析该列表并创建一个表或直接连接源表,那么结果集仍然会被过滤掉,而不是整个查询。

Depending on what percentage of the list you're going to exclude, how much other data comes along with it, how loaded the database is and how slow your network is, it is usually going to work out better if you filter at the source instead of pulling down a bunch of data just to discard it.

In SQL Server 2008, you could go the opposite way - pass your list into a table-valued parameter, and perform the filter on the server.

In SQL Server 2005, you could use slightly less efficient ways to simulate the same thing - pass a comma-separated list of values into the stored procedure, the stored procedure then parses the list and makes a table or directly joins against the source table, then the resultset is still filtered down instead of the whole query.

遇到 2024-12-09 04:06:53

像这样的事情怎么样:

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

更多详细信息在这里 how-do -将数据表转换为通用列表

How about something like this:

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

More details here how-do-you-convert-a-datatable-into-a-generic-list

夏末 2024-12-09 04:06:52

您应该做的是在该 cmd 对象上调用 ExecuteReader(),如下所示:

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,只需将索引调整为您希望在其中找到它们的列。

此解决方案还期望命令的连接已打开。如果不是,您可以在 cmd.ExecuteReader() 之前执行 cmd.Connection.Open()

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);
            }
        }
    }
}

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.

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文