oracle 无法序列化数据表。未设置数据表名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35314515/
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 03:09:28  来源:igfitidea点击:

Cannot serialize the DataTable. DataTable name is not set

c#sqloracledatatable

提问by Nime Cloud

I need to export all datatables to individual XML files and I cannot export all the rows at once because of System.OutOfMemoryException if there is a huge table. So I tried to export N rows. However WriteXml throws an exception if I use paging syntax in query.

我需要将所有数据表导出到单独的 XML 文件,如果有一个巨大的表,我无法一次导出所有行,因为 System.OutOfMemoryException。所以我尝试导出 N 行。但是,如果我在查询中使用分页语法,WriteXml 会引发异常。

I've tested both queries in LINQPad, they are ok.

我已经在 LINQPad 中测试了这两个查询,它们没问题。

System.InvalidOperationException: Cannot serialize the DataTable. DataTable name is not set.

System.InvalidOperationException:无法序列化数据表。未设置数据表名称。

  // The first query causes exception
  string oraQueryGetTableData = "SELECT * FROM (SELECT t0.* FROM MY_TABLE t0) WHERE ROWNUM <= 100";

  // The second query runs without any error
  //oraQueryGetTableData = "SELECT * FROM MY_TABLE";

  OracleCommand oraCommandGetTableData = new OracleCommand(oraQueryGetTableData, oraConnection);

  OracleDataReader oraReaderTableData = oraCommandGetTableData.ExecuteReader();

  DataTable dataTable = new DataTable();
  dataTable.Load(oraReaderTableData);

  // Exception might occur here
  dataTable.WriteXml(writer, true);  

What is the problem here or how can I fix this?

这里有什么问题或者我该如何解决这个问题?

回答by Jens Meinecke

As the exception says - the table does not have it's TableNameproperty set. So you just need to set it.

正如例外所说 - 该表没有TableName设置它的属性。所以你只需要设置它。

DataTable dataTable = new DataTable { TableName = "MyTableName"};
dataTable.Load(oraReaderTableData);
....

回答by Pranithan T.

If you use Newtonsoftto Deserialize the JSON string data to be DataTable, the TableNameattribute will NOTbe set. I composed the sample code and put the break point after the dtassignment to let you see what is dt.TableName. The debugging result is shown below:

如果您使用Newtonsoft将 JSON 字符串数据反序列化为DataTableTableName不会设置该属性。我编写了示例代码并在dt赋值之后放置了断点,让您看看dt.TableName. 调试结果如下图所示:

Sample Code

示例代码

So, if you would like to return the DataTableas a result of web service method, the TableNameattribute assignment is needed after DeserializeObject. I change the code to be:

因此,如果您想返回DataTable作为 Web 服务方法的结果,TableName则需要在 之后进行属性分配DeserializeObject。我将代码更改为:

DataTable dt = JsonConvert.DeserializeObject<DataTable>(
@"[
    { col1: 'value1', col2: 'value2' },
    { col1: 'value3', col2: 'value4' },
    { col1: 'value5', col2: 'value6' },
]");
dt.TableName = "TableName";

And I debug it again. This is The preferable DataTablethat can be returned by a web method.

我再次调试它。这是DataTable可以通过 Web 方法返回的首选方法。

Sample Code 2

示例代码 2