'System.Data.DataRow'不包含'GetEnumerator'的公共定义



我正在尝试连接到我在sqlservermanagement上建立的数据库

我已经在Visual Studio 2012上制作了一个控制台应用程序

我有这个声明

  foreach (DataRow pRow in persoaneSet.Tables["persoane"].Rows)
  {
    Console.Write("t{0}t{1}t{2}", pRow["nrcrt"],pRow["nume"],pRow["codc"]);
    foreach (DataRow cRow in pRow.GetParentRow(relPersCrt)) // **here is the error**
    {
        Console.WriteLine("t{0}t{1}t{2}", cRow["autor"],cRow["titlu"],cRow["cota"]);
    }
  }
在这里

,我必须从我的数据库中将表1连接到表2,在这里是表1,我像我的老师一样制作,但不幸的是我收到了错误

foreach 语句不能对类型为 'System.Data.DataRow' 的变量进行操作,因为 'System.Data.DataRow' 不包含 'GetEnumerator' 的公共定义

我必须在这里做什么?

老师的例子,它正在工作,但不是我的

您不需要foreach,因为只有一个父行。尝试:

foreach (DataRow pRow in persoaneSet.Tables["persoane"].Rows)
{
    Console.Write("t{0}t{1}t{2}", pRow["nrcrt"],pRow["nume"],pRow["codc"]);
    DataRow cRow = pRow.GetParentRow(relPersCrt);
    Console.WriteLine("t{0}t{1}t{2}", cRow["autor"],cRow["titlu"],cRow["cota"]);
}

此外,您应该考虑添加额外的检查,因为您的代码很容易出现未经处理的异常。

您只能将foreach与实现IEnumerable接口的东西一起使用。基本上,您只能迭代一组事物,而不能迭代单个事物。第一个 foreach 循环访问一组行,第二个 foreach 尝试迭代单个行。

相关内容

最新更新