【问题标题】:Returning Data Rows to List<string> with SqlDataReader使用 SqlDataReader 将数据行返回到 List<string>
【发布时间】:2016-06-27 01:42:24
【问题描述】:

我正在尝试创建一个通用 SqlDataReader,它将具有 33 列的表转换为列表。我希望每个列表项包含每行的所有 33 个列值。

但是,我的代码将每个值分配给单个列表项。

因此,我有 33,000 个列表项,而不是 1000 个列表项 = 1000 行数据。

我更喜欢使用列表而不是数据表,因为我需要做的列表比较要简单得多。

如何拥有 1000 个列表项,每个列表项有 33 个值?

   public static List<string> loadSQL(String query, String connectString)
    {
        List<string> dataList = new List<string>();

        using (SqlConnection connection = new SqlConnection(connectString))
        {
            using (SqlCommand command = new SqlCommand(query, connection))

            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {

                        for (int i = 0; i < reader.FieldCount; i ++) 
                            { 
                                dataList.Add(Convert.ToString(reader.GetValue(i)));
                            }
                    }
                }
            }
        return dataList;

        }
    }

...更新...

更正如下。它正确返回列表项。但是,我的列表包含 33,000 个项目,每个项目包含 33 个项目。如何控制循环使其在 1000 行后停止?

        using (SqlConnection connection = new SqlConnection(connectString))
        {
            using (SqlCommand command = new SqlCommand(query, connection))

            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {

                       List<string> tempRow = new List<string>();
                        for (int i = 0; i < reader.FieldCount; i ++) 
                            { 
                                tempRow.Add(Convert.ToString(reader.GetValue(i)));
                            }
                        dataList.Add(tempRow);

                    }
                }
            }
        }

【问题讨论】:

  • 顺便说一句,您在处理您的 connection 之前返回 dataList 。

标签: c# .net sqldatareader


【解决方案1】:

执行此任务的最佳选择是 DataTable,但您不想使用它。因此,net 选项将是,基于查询输出创建一个类,然后使用List&lt;objectOftheClass&gt;。但是在您的情况下,输入查询将一直更改,因此通用类将没有意义,因为您试图使其通用。因此,您可以选择List&lt;List&lt;string&gt;&gt; 或List&lt;List&lt;object&gt;&gt;。按照这个方法签名将如下所示:

public static List<object[]> loadSQL(string query, string connectString)
{
    List<object[]> dataList = new List<object[]>();

    using (SqlConnection connection = new SqlConnection(connectString))
    {
        using (SqlCommand command = new SqlCommand(query, connection))
        {
            connection.Open();

            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    object[] tempRow = new object[reader.FieldCount];
                    for (int i = 0; i < reader.FieldCount; i++)
                    {
                        tempRow[i] = reader[i];
                    }
                    dataList.Add(tempRow);
                }
            }
        }
    }
    return dataList;
}

为什么是List&lt;object&gt;?为什么不`List?:

阅读器会给你列数据作为表中相同类型的列。如果是对象,则不需要每次都转换。

** 注意:-** 将方法签名中的参数的String 更改为string。可以找理由here

【讨论】:

  • 与下面的另一个答案一样,使用辅助列表不是最佳答案,因为 FieldCount 是固定的,因此您可以使用数组。 List 或 List 取决于您希望如何存储结果。
  • 感谢您的反馈。它正确返回列表项。但是,我的列表包含 33,000 个项目,每个项目包含 33 个项目。如何控制循环,使其在 1000 行后停止
  • @Richard :我有点困惑,希望当前的代码能给你 1000 个列表项,每个项包含 33 个子项,对吗?那为什么要在 1000 行之后停止呢?
  • @un-lucky 我的数据库只包含 1000 行。我想每行有一个列表项,然后是代表列的 33 个子项
  • @GaryHolland:是的,你是对的。我已经更新了答案。现在让我知道否决票有什么意义吗?
【解决方案2】:

您可以像这样使用List&lt;List&lt;string&gt;&gt;:

public static List<List<string>> loadSQL(String query, String connectString)
    {
        List<List<string>> dataList = new List<List<string>>();

        using (SqlConnection connection = new SqlConnection(connectString))
        {
            using (SqlCommand command = new SqlCommand(query, connection))

            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var l = new List<string>();
                        for (int i = 0; i < reader.FieldCount; i ++) 
                            { 
                                l.Add(Convert.ToString(reader.GetValue(i)));
                            }
                        dataList.Add(l);
                    }
                }
            }
        return dataList;

        }
    }

【讨论】:

  • 我会建议 List 因为 FieldCount 是固定的,所以没有理由需要第二个动态数组的开销。
  • 对不起,我应该更清楚。 FieldCount 在未来的实现中需要是可变的。
【解决方案3】:

您可以使用列表中的数组来实现您想要做的事情。这是一个使用您的代码的简单示例:

public static List<string[]> loadSQL(String query, String connectString)
{
List<string[]> dataList = new List<string[]>();

using (SqlConnection connection = new SqlConnection(connectString))
{
    using (SqlCommand command = new SqlCommand(query, connection))

    {
        connection.Open();

        using (SqlDataReader reader = command.ExecuteReader())
        {
            int rowcounter = 0;

            while (reader.Read())
            {
                string[] value = new string[reader.FieldCount];

                for (int i = 0; i < reader.FieldCount; i ++) 
                    { 
                        value[i] = Convert.ToString(reader.GetValue(i));
                    }

                dataList.Add(value);
                rowcounter++;
            }
        }
    }
return dataList;

}

提醒一下,如果你想使用列表,你需要将值嵌入单个字符串,使用逗号分隔符或类似的东西。

【讨论】:

  • 您可以将其作为新问题发布,因为它包含大量语法错误。喜欢object[i]=...
  • 你测试了吗?您的答案中仍然存在语法错误。与其否决其他答案,请先更正您的答案
  • 是的。对您的回答感兴趣,但我在 dataList.Add(value); 上收到错误“当前上下文中不存在名称‘值’”;知道为什么吗?
  • 抱歉,现在在手机上无法测试。我认为错误是由于使用 object[] 而不是 string[]。立即尝试,当我回到计算机时,我会正确修复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 2016-09-27
  • 1970-01-01
  • 2012-12-26
相关资源
最近更新 更多