0

我有以下示例,其中SourceData该类将表示由 Sql 查询产生的 DataView:

class MainClass
{
    private static SourceData Source;
    private static DataView View;
    private static DataView Destination;
        
    public static void Main (string[] args)
    {
        Source = new SourceData();
        View = new DataView(Source.Table);
        Destination = new DataView();
            
        Source.AddRowData("Table1", 100);
        Source.AddRowData("Table2", 1500);
        Source.AddRowData("Table3", 1300324);
        Source.AddRowData("Table4", 1122494);
        Source.AddRowData("Table5", 132545);

        Console.WriteLine(String.Format("Data View Records: {0}", View.Count));         

        foreach(DataRowView drvRow in View)
        {
            Console.WriteLine(String.Format("Source {0} has {1} records.", drvRow["table"], drvRow["records"]));
            DataRowView newRow = Destination.AddNew();
            newRow["table"] = drvRow["table"];
            newRow["records"] = drvRow["records"];
        }
            
        Console.WriteLine();
        Console.WriteLine(String.Format("Destination View Records: {0}", Destination.Count));
            
        foreach(DataRowView drvRow in Destination)
        {
            Console.WriteLine(String.Format("Destination {0} has {1} records.", drvRow["table"], drvRow["records"]));
        }

    }
}

class SourceData
{
    public DataTable Table
    {
        get{return dataTable;}
    }
        
    private DataTable dataTable;
    
    public SourceData()
    {
        dataTable = new DataTable("TestTable");
        dataTable.Columns.Add("table", typeof(string));
        dataTable.Columns.Add("records", typeof(int));
    }
        
    public void AddRowData(string tableName, int tableRows)
    {
        dataTable.Rows.Add(tableName, tableRows);
    }
}

我的输出是:

数据查看记录:5
源表 1 有 100 条记录。

未处理的异常:System.NullReferenceException:对象引用未设置到 /usr/src/packages/BUILD/mono-2.4.2.3 /mcs/class/ 中 System.Data.DataView.AddNew () [0x0003e] 处的对象实例System.Data/System.Data/DataView.cs:344 在 /home/david/Projects/DataViewTest/SourceData.cs:29 中的 DataViewTest.MainClass.Main (System.String[] args) [0x000e8]

我在这里做了一些阅读:DataView:AddNew Method...
...看来我这样做是正确的。为什么我没有设置对象引用?

4

2 回答 2

0

DataView必须是对某事的看法。你得到它的方式,它不是任何东西的视图(使用默认构造函数) - 你至少需要将DataView.Table属性设置为某些东西。

我认为您实际上想DataTable为“目的地”创建一个新的,而不是DataView.

于 2010-05-27T02:59:22.790 回答
-1

问题在于Destination数据视图的声明。为了使AddNew()to 起作用,必须有一个对象(在本例中为 DataTable)来引用和添加新记录。

通过更正以下内容:

Destination = new DataView(new SourceData().Table);

...the dataview will have it's requirements met. This may not make sense at first - but realize that the DataView class isn't really complete until it has a DataTable (or some other form of record set) on which to build.

The output is now:

Data View Records: 5
Source Table1 has 100 records.
Source Table2 has 1500 records.
Source Table3 has 1300324 records.
Source Table4 has 1122494 records.
Source Table5 has 132545 records.

Destination View Records: 5
Destination Table1 has 100 records.
Destination Table2 has 1500 records.
Destination Table3 has 1300324 records.
Destination Table4 has 1122494 records.
Destination Table5 has 132545 records.

于 2010-05-27T03:00:10.323 回答