简体   繁体   English

如何添加清单 <object> 使用NpgsqlDataReader()C#

[英]How add list<object> with NpgsqlDataReader() C#

I do not know what I am doing wrong, when I add the values ​​from my database to a list<object> it always returns the list with the total of objects but all the values ​​are those of the last record that was made in the loop while . 我不知道我在做什么错,当我将数据库中的值添加到list<object>它总是返回包含对象总数的列表,但所有值都是最后一个记录的值在循环中进行while

This is my code: 这是我的代码:

public List<object> getdata(string storedProcedure)
    {
        List<object> list = new List<object>();
        try
        {
            using (var conn = new NpgsqlConnection(connstring))
            {
                conn.Open();
                NpgsqlTransaction tran = conn.BeginTransaction();
                NpgsqlDataReader reader;
                var cmd = new NpgsqlCommand(storedProcedure, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                reader = cmd.ExecuteReader();

                int fieldCount = reader.FieldCount;
                object[] fieldValues = new object[fieldCount];
                while (reader.Read())
                {
                    int instances = reader.GetValues(fieldValues);
                    for (int fieldCounter = 0; fieldCounter < fieldCount; fieldCounter++)
                    {
                        if (Convert.IsDBNull(fieldValues[fieldCounter]))
                            fieldValues[fieldCounter] = "NA";                               
                    }
                    list.Add(fieldValues);
                }                       
                reader.Close();                    
                tran.Commit();
                conn.Close();
                return list;
            }
        }
        catch (Exception ex)
        {
        }
        return list;
    }

This is what I get in all positions, it is the last value: 这是我在所有职位上得到的,这是最后一个值: 在此处输入图片说明

You need to move the declaration and initialization of your object array inside the loop 您需要在循环内移动对象数组的声明和初始化

  ....
  while (reader.Read())
  {
      object[] fieldValues = new object[fieldCount];
      ....
  }

The problem that you experience is caused by the fact that when you initialize the array outside the loop and reuse it at every loop of the datareader, you replace the previous content with the content of the current record. 您遇到的问题是由以下事实引起的:当您在循环外初始化数组并在数据读取器的每个循环中重用它时,您会将前一个内容替换为当前记录的内容。
But when you add the array to the list of objects you add the same reference to initial array where only the content has been changed. 但是,当您将数组添加到对象列表时,会将相同的引用添加到仅更改了内容的初始数组。 Obviously when you reach the last record there is only one array while the list contains n reference to the same memory area. 显然,当您到达最后一条记录时,只有一个数组,而列表包含对同一存储区的n个引用。 So you display n time the same last record. 因此,您显示n次相同的最后一条记录。

Moving the initialization inside the loop provides the list with a new reference at each loop and each reference maintain the data of the record received during the loop. 在循环内移动初始化为列表提供了每个循环的新引用,并且每个引用都维护循环期间接收到的记录数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM