Microsoft.Jet.OLEDB.4.0 Converting Characters

元气小坏坏 提交于 2019-12-28 18:14:30

问题


I'm working with a CSV that contains characters like:

” and •

I am reading the CSV via OleDb and the provider is Microsoft.Jet.OLEDB.4.0. when the data is loaded into the OleDbCommand, the characters are converted to the following respectively:

“ and •

I suspected there might be a collation setting in the connection string but I was unable to find anything about this.

I can confirm the following:

  1. I can see the original character in the CSV when I open it.
  2. If I run a select on the file through OleDb WHERE [field] LIKE '%•%' I get 0 rows but if SELECT WHERE [field] LIKE '%“%' I get rows returned.

Any thoughts?


回答1:


Finally! Thanks to @HABJAN I was able to get to the resolution which is as simple as setting the CharaterSet in the Extended Properties of the connection string. For my situation it was UTF-8...commonly used by default in PHPMyAdmin which is where my data was retrieved from.

Resulting working connection string:

"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\";Extended Properties=\"text;HDR=Yes;FMT=Delimited;CharacterSet=65001;\""

Key is CharacterSet=65001 (Code Page Identifier for UTF-8) which might have been obvious to some collation savvy individuals but I've somehow managed to avoid these issues over the years and never come across it in this respect.

I was also able to get HABJAN's solution to work when also following the documentation found @ http://msdn.microsoft.com/en-us/library/ms709353%28v=vs.85%29.aspx and setting the CharacterSet to the same as above.

For my situation, this is the better method as it is a simpler/more maintainable solution, but +1 to HABJAN for helping me get there!

Thanks




回答2:


You can create schema.ini file and play with format and CharacterSet properties.

Take a look at this sample: How to read data from Unicode formatted text file and import to Data Table using .Net

And here is another sample that will show you how to read csv file with schema.ini: Importing CSV file into Database with Schema.ini



来源:https://stackoverflow.com/questions/5794072/microsoft-jet-oledb-4-0-converting-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!