oracle 将大型 CLOB 对象转换为 .NET 字符串以放入 DataGridView 单元格

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

Converting a large CLOB object to .NET string to put into a DataGridView cell

c#.netoracledatagriddatagridview

提问by Pierre Gardin

I have created a Windows Forms GUI in C# that can display tabular data (results of an SQL query against an Oracle Server DB) in a DataGridView. One of the field is an XML, potentially quite large, stored as a CLOB (Character Large OBject if I'm right). Of course, the user will not directly look at the cell, he will rather double-click the cell to see the file pretty-printed. The problem is that I need to convert the file (which is a CLOB) to a .NET String otherwise it generates an exception. I have tried, as a workaround, to use the Oracle to_char procedure, but it is limited to 4000 characters. So I take a substring of the file like this:

我在 C# 中创建了一个 Windows 窗体 GUI,它可以在 DataGridView 中显示表格数据(针对 Oracle Server DB 的 SQL 查询的结果)。其中一个字段是 XML,可能非常大,存储为 CLOB(如果我是对的,则为字符大对象)。当然,用户不会直接查看单元格,而是双击单元格以查看打印精美的文件。问题是我需要将文件(它是 CLOB)转换为 .NET 字符串,否则会生成异常。作为一种变通方法,我尝试使用 Oracle to_char 过程,但它被限制为 4000 个字符。所以我像这样取文件的子字符串:

 select to_char(dbms_lob.substr(column_name, 4000, 1 ))

The problem is, it doesn't display the whole file if it contains more than 4000 characters. How could I circumvent this limitation?

问题是,如果它包含超过 4000 个字符,它不会显示整个文件。我怎样才能绕过这个限制?

回答by hometoast

Don't store the CLOB, or the resultant string in the DataGridView.

不要在 DataGridView 中存储 CLOB 或结果字符串。

Instead, capture the click event in the DataGridView then convert the CLOB to a string for viewing. Use an appropriate encoding from System.Text.Encoding.

相反,在 DataGridView 中捕获单击事件,然后将 CLOB 转换为字符串以供查看。使用适当的编码System.Text.Encoding

I'm assuming your DataReader or DataAdapter (whichever way you're filling the DataSet) will store your CLOB in a byte-array. (As it is with SQLServer and Informix drivers).

我假设您的 DataReader 或 DataAdapter(无论您填充 DataSet 的方式如何)都将您的 CLOB 存储在一个字节数组中。(就像 SQLServer 和 Informix 驱动程序一样)。

byte[] clob;
// get it from your datarow/datagridview bound item
string thexml = System.Text.Encoding.UTF8.GetString(theclob)

回答by Pierre Gardin

I used the simple:

我使用了简单的:

if (reader.IsDBNull(i))
{
    cellValue = "NULL";
}
else
{
    OracleLob clob = reader.GetOracleLob(i);
    cellValue  = (string) clob.Value;
}