简体   繁体   English

使用C#,是否可以使System.Data.OleDb处理特殊字符?

[英]Using C#, is it possible to make System.Data.OleDb handle special characters?

I have an existing C# project that reads a text file and loads it into an Oracle DB using System.Data.OleDb as provider. 我有一个现有的C#项目,它读取文本文件并使用System.Data.OleDb作为提供程序将其加载到Oracle DB中。 Problem is that if the file has upper ascii characters (eg ÀÑÓ, non-breaking space) it always generates an error when it tries to load it into Oracle: 问题是,如果文件具有上部ascii字符(例如ÀÑÓ,非中断空格),它在尝试将其加载到Oracle时总是会生成错误:

Error is: OLEDBConnection Command Parameter data value could not be converted for reasons other than sign mismatch or data overflow . 错误是: OLEDBConnection命令由于符号不匹配或数据溢出以外的原因,无法转换参数数据值

Our Oracle can accept upper ascii characters (insert via SQL*PLUS works fine), it is System.Data.OleDb that is having issue. 我们的Oracle可以接受上面的ascii字符(通过SQL * PLUS插入工作正常),它是有问题的System.Data.OleDb。

Anyone know if there is a setting to change this? 有人知道是否有改变这个的设置? Can't believe that that only accepts AZ-0-9. 不敢相信那只接受AZ-0-9。 Looked thru a ll documentation but couldn't find anything. 通过ll文档查看,但找不到任何东西。

If it can't, how do you let OLEDB know to escape the character. 如果它不能,你怎么让OLEDB知道逃脱角色。 Tired putting \\ in the file before special characters, but it still errors with same message. 在特殊字符之前将文件放在文件中,但它仍然存在相同消息的错误。

Usually this is possible without any problem... since you don't provide much detail only some general pointers: 通常这是可能的,没有任何问题...因为你没有提供太多细节只有一些一般的指针:

  • Database Charset should be set to AL32UTF8 Database Charset应设置为AL32UTF8
    you can check this by executing SELECT parameter, value FROM nls_database_parameters WHERE parameter = 'NLS_CHARACTERSET'; 您可以通过执行SELECT parameter, value FROM nls_database_parameters WHERE parameter = 'NLS_CHARACTERSET';来检查这一点SELECT parameter, value FROM nls_database_parameters WHERE parameter = 'NLS_CHARACTERSET';
  • Client Charset should be set to AL32UTF8 客户端字符集应设置为AL32UTF8
    see for this registry settings for NLS_LANG 请参阅NLS_LANG的此注册表设置
  • Columns should be VARCHAR2 列应为VARCHAR2
  • Connection string should include OLEDB.NET=True 连接字符串应包含OLEDB.NET=True
    for further reference see http://download.oracle.com/docs/cd/B28359_01/win.111/b28431/using.htm#i1017221 有关进一步参考,请参阅http://download.oracle.com/docs/cd/B28359_01/win.111/b28431/using.htm#i1017221

Other points to check could be OS and Client and OLEDB versions... some have bugs or strange behaviour... 其他要检查的点可能是操作系统和客户端以及OLEDB版本......有些还有错误或奇怪的行为......

You may have to use HEX conversions. 您可能必须使用HEX转换。 For example %c4 is Ä. 例如,%c4是Ä。

Try that and let me know if that works. 试试看,如果有效,请告诉我。 Here are the conversion functions you can use. 以下是您可以使用的转换功能。

internal String convertAsciiTextToHex(String asciiText)
{
StringBuilder sBuffer = new StringBuilder();
for (int i = 0; i < asciiText.Length; i++)
{
    sBuffer.Append(Convert.ToInt32(asciiText[i]).ToString("x"));
}
return sBuffer.ToString().ToUpper();
}

internal String convertHexToAsciiText(String hexString)
{
    StringBuilder sb = new StringBuilder();

 for (int i = 0; i < hexString.Length; i += 2)
 {
   string hs = hexString.Substring(i, 2);
   sb.Append(Convert.ToChar(Convert.ToUInt32(hs, 16)));
 }
    String ascii = sb.ToString();
    return ascii;
}

您的数据类型和参数类型是nvarchar2吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#使用System.Data.OleDb的问题 - C # using System.Data.OleDb problem C#WebApplication-找到System.Data.OleDb但几乎为空 - C# WebApplication - System.Data.OleDb found but nearly empty 即使使用 System.Data.OleDb,C# 也找不到命名空间 OleDbConnection - C# cannot find namespace OleDbConnection even though using System.Data.OleDb is there System.Data.OleDb替代System.Data.OracleClient(C#) - System.Data.OleDb as alternative to System.Data.OracleClient (C#) Vidual Studio 2017 C# System.Data.OleDb 无法读取较新的 Excel 文件 - Vidual Studio 2017 C# System.Data.OleDb cannot read newer Excel files 在代码中使用System.Data.OleDb命名空间时找不到OleDbConnection - OleDbConnection not found while using System.Data.OleDb namespace in code PlatformNotSupportedException:此平台不支持 System.Data.OleDb - PlatformNotSupportedException: System.Data.OleDb is not supported on this platform 在命名空间“System.Data.OleDb”中找不到类型名称“OleDbDataAdapter” - Type name "OleDbDataAdapter" could not be found in the namespace "System.Data.OleDb" 缺少System.Data.OleDb-改用什么 - System.Data.OleDb Missing - What to use instead Xamarin的System.Data.dll 2.0.5.0的System.Data.OleDb中缺少OleDbConnection - OleDbConnection missing in System.Data.OleDb on System.Data.dll 2.0.5.0 for Xamarin
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM