oracle 为什么 DataRow 在方法的一部分中被识别而不是在另一部分中被识别(如何动态添加 DataRow)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12081875/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:04:23  来源:igfitidea点击:

Why is a DataRow recognized in one part of a method but not another (how can I dynamically add DataRows)?

c#winformsoracledynamicdotconnect

提问by B. Clay Shannon

With this code:

使用此代码:

OracleDataTable dt = PlatypusSchedData.GetAvailablePlatypi(); 
OracleDataTable outputDt = new OracleDataTable();

int iRows = 12;
while (iRows > 0)
{
    outputDt.Rows.Add(new DataRow()); // line 1
    //var dr = new DataRow();     // line 2a
    //outputDt.Rows.Add(dr);      // line 2b
    iRows -= 1;
}

for (int i = 0; i < dt.Rows.Count; i += 1) {
    DataRow dr = dt.Rows[i];
    int outputColumn = 0;
    if (i % 12 == 0 && i > 0) {
        outputColumn += 1; //2?
    }
    outputDt.Rows[i % 12][outputColumn] = dr[0];
    outputDt.Rows[i % 12][outputColumn + 1] = dr[1];
}
dataGridView1.DataSource = outputDt;

...I get this compile-time error using either line 1 (lines 2a and 2b commented out) or using lines 2a and 2b (with line 1 commented out):

...我使用第 1 行(注释掉第 2a 行和第 2b 行)或使用第 2a 行和第 2b 行(注释掉第 1 行)得到这个编译时错误:

'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level

'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' 由于其保护级别而无法访问

This is baffling me because the DataRow in the for loop is tolerated. How can I add these DataRows to my OracleDataTable?

这让我很困惑,因为 for 循环中的 DataRow 是可以容忍的。如何将这些 DataRow 添加到我的 OracleDataTable?

回答by Steve Czetty

The constructor for DataRowis marked as protected internal, and as such, you cannot construct it directly.

for 的构造函数DataRow被标记为protected internal,因此您不能直接构造它。

The correct code to get a new row for a DataTableis

为 a 获取新行的正确代码DataTable

DataRow dr = dt.NewRow();

回答by Tim Schmelter

I'm not familiar with OracleDataTablebut i assume that it inherits from a normal DataTable.

我不熟悉,OracleDataTable但我认为它继承自普通的DataTable.

You cannot create a DataRowdirectly because the constructor is protected. Instead you must use one of the factory methods like DataTable.Rows.Addor DataTable.NewRow. This ensures that the correct schema is applied on the new DataRowbecause it's determined from its DataTable.

您不能DataRow直接创建,因为构造函数是protected。相反,您必须使用工厂方法之一,例如DataTable.Rows.Addor DataTable.NewRow。这可确保将正确的架构应用于新架构,DataRow因为它是从其DataTable.

So this should work:

所以这应该有效:

while (iRows > 0)
{
    DataRow dr = outputDt.NewRow();
    // fill the fields of the row ...
    // add it to the DataTable:
    outputDt.Rows.Add(dr);
    iRows -= 1;
}