Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C-尝试将行值添加到列表会引发InvalidOperationException_C#_Enumerable_Getvalue - Fatal编程技术网

C# C-尝试将行值添加到列表会引发InvalidOperationException

C# C-尝试将行值添加到列表会引发InvalidOperationException,c#,enumerable,getvalue,C#,Enumerable,Getvalue,我想将列的值(即列名)和特定行的行值存储到两个单独的列表中。提供者和文档都在列表中。但由于与文档存在类型冲突,我使用的是var-Document,所以编译器接受它 在var Document=Enumerable.Range0,dr.FieldCount.Selectdr.GetValue.ToList之前,下面的代码工作得非常好 第一个列表正确存储了列名,但另一个列表抛出InvalidOperationException,并表示行或列中都没有值。这对于某些列是正确的,因为它们是空的。我用另一个

我想将列的值(即列名)和特定行的行值存储到两个单独的列表中。提供者和文档都在列表中。但由于与文档存在类型冲突,我使用的是var-Document,所以编译器接受它

在var Document=Enumerable.Range0,dr.FieldCount.Selectdr.GetValue.ToList之前,下面的代码工作得非常好

第一个列表正确存储了列名,但另一个列表抛出InvalidOperationException,并表示行或列中都没有值。这对于某些列是正确的,因为它们是空的。我用另一个DB表测试了它,其中每个列/行都有一个值,但我在这里得到了相同的异常

我做错了什么

提前谢谢

编辑:

下面是两个屏幕截图,显示了读卡器读取的值

第一个列名称值为灰色,它是敏感数据:

第二个包含整行的值:

您需要调用dr.Read读取结果的第一行

另外,代码中的文档是一个被丢弃的局部变量。我猜您想将其分配给同名的属性?您还需要将dr.GetValue的结果强制转换为字符串

我认为您需要初始化Document和Provider以及call AddRange方法,而不是将它们分配给结果。

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace ConsoleApplication57
{
    class Program
    {
        static void Main(string[] args)
        {
            OleDbConnection conn = new OleDbConnection("Enter Connection String Here");
            string abfrage = "Enter Query Here";
            OleDbCommand cmd = new OleDbCommand(abfrage, conn);
            OleDbDataReader dr;
            List<string> Provider = new List<string>();
            int colIndex = 3;
            try
            {
                conn.Open();

                dr = cmd.ExecuteReader();
                while (dr.Read())
                {

                    Provider.Add(dr.GetString(colIndex));
                }

                dr.Close();
                dr.Dispose();
            }

            catch (System.InvalidOperationException inv)
            {
                MessageBox.Show(inv.Message);
                throw;
            }
        }
    }
}

我在dr.Read{Provider.Adddr.GetStringcolIndex;}时尝试过,但它再次抛出相同的异常。我真的不明白,因为第一个列表得到了正确的结果。这似乎是在向提供程序读取所有行,而OP只想读取一行中的所有值。@RobinSalih是的,正确。我的查询在一行中查找一个特定的值,比如ID 5,并返回包含所有列值的关联行。ExcuteReader不会返回任何行。连接或查询命令文本有问题。我将在OP中发布两个屏幕截图,以便您可以看到读卡器读取的值。我在dr.read{//var Document=Enumerable.Range0,dr.FieldCount.Selectdr.GetValue.ToList;}时尝试了此操作实际上,它将值存储/添加到var文档中。而且,正如您所说,文档会被丢弃。我试着投下它,但我不知道如何管理它。我还尝试了你的建议,调用AddRange方法,但没有成功。我想我明白了。此代码正在工作,文档保存结果:IEnumerable文档;和Document=Enumerable.Range0,dr.FieldCount.Selectdr.GetValue.ToList;。这是正确的/最好的方法吗?@Allix,是的,这样做很好。您可以使文档成为IList。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace ConsoleApplication57
{
    class Program
    {
        static void Main(string[] args)
        {
            OleDbConnection conn = new OleDbConnection("Enter Connection String Here");
            string abfrage = "Enter Query Here";
            OleDbCommand cmd = new OleDbCommand(abfrage, conn);
            OleDbDataReader dr;
            List<string> Provider = new List<string>();
            int colIndex = 3;
            try
            {
                conn.Open();

                dr = cmd.ExecuteReader();
                while (dr.Read())
                {

                    Provider.Add(dr.GetString(colIndex));
                }

                dr.Close();
                dr.Dispose();
            }

            catch (System.InvalidOperationException inv)
            {
                MessageBox.Show(inv.Message);
                throw;
            }
        }
    }
}