C# OleDb Connection to mdb File with changed character set to UTF8 - c#

i have a problem to change Character set to UTF8 when i ma connecting to my mdb file.
I can connect to this, the only problem is it has some š,ž,č etc. characters in it. So i wanted to change character set in my connectionString to UTF8.
My connectionString looks like this:
string conectionString = "Provider=Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;";
This connection string will connect me just fine. But when i add this:
string conectionString = "Provider=Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;Extended properties='Character set=UTF8;'";
it will throw an error : Could not find installable isam. I was trying to find more about it but only thig that i found was about single quotes.
I know there is a lot of questions about this but I go through a lot of them.
Thanks for any help.

I Know that this is too late, but for the people that want to know the answer.
I'm using some more for the Extend Properties, I rescue it from ChatGPT but I add the knowledge that I got.
For your location, has to be the folder that contains your file, after you can do a query like "SELECT * FROM [/Name of your file with extention/]"
The correct format to the connection would be Provider=Microsoft.Jet.OLEDB.4.0;Data Source=/*Your location*/;Extend Properties="text;HDR=Yes;FMT=Delimited;'CharacterSet=65001'"
In your string variable, you can use:
string connectionString= $#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={/*Your location*/};Extended Properties=""text;HDR=Yes;FMT=Delimited;'CharacterSet=65001'""";
I'm using the template string and the # for get the scape of '"'

Related

How do I handle the whitespaces of a registry path?

I'm creating a process in my application that makes it possible to run commands like you do it in the command prompt.
When I want to export a registry key, I use REG EXPORT path file to access the registry key and export the information into the file. If I use a path which contains no whitespaces at all, everything works fine. But as soon as I use a path with whitespaces, e.g. HKEY_CURRENT_USER\Software\NVIDIA Corporation, it does not work.
I'm appending the path as a String with a StringBuilder to the command, the String itself looks like this: String path = "HKEY_CURRENT_USER\\Software\\NVIDIA Corporation".
Do I have to change the String contained in path? Or is there a special static method I can use to format the String?
You probably need to surround the path with quotes.
This might help:
String path = "\"HKEY_CURRENT_USER\\Software\\NVIDIA Corporation\"";

C# - Illegal characters in path

I have a database table that containing file paths of excel files that I import using a C# script.
The script works fine unless the filepath contains spaces e.g. C:\Temp\My Excel File.xls and I get an Illegal characters in path error message. Unfortunately I am not able to change the file names at the source.
If I hard code the file path to be as below it works fine.
String Filepath = #"C:\Temp\My Excel File.xls";
How do I alter this so I can include a string variable that will store the filepath from the database e.g.
String Filepath = //Code to get FilePath from database
StringCorrectedFilePath = #+FilePath;
Thanks in advance of any help
Edit: Issue is caused by files that start with a number creating invalid escape sequence. e.g. C:\Temp\20160611 My Excel File.xls
Edit 2: SOLVED - Error was caused by carriage return characters appearing after the file extension. Please see my answer for the solution.
Whether you do this
String Filepath = #"C:\Temp\My Excel File.xls";
or this
String Filepath = "C:\\Temp\\My Excel File.xls";
the string stored in memory is just C:\Temp\My Excel File.xls, whatever the debugger may tell you. So when you read some string from somewhere (database, file, user input, ...) you don't need to "escape" backslashes. So just use that string.
Path.GetInvalidFileNameChars
FilePath = string.Concat(FilePath.Split(System.IO.Path.GetInvalidFileNameChars())).Trim();
Well you can replace blank space with %20 character and while retrieving replace back with blank space again like (you may as well choose to use regular expression for the same)
String Filepath = #"C:\Temp\My Excel File.xls";
Filepath = Filepath.Replace(" ", "%20");
While retrieving back
string mypath = pathyouhavegotfromDB.Replace("%20", " ");
I think you need to put quotation marks around the path with spaces.
string filepath = #"C:\Temp\My Excel File.xls";
filepath = $"\"{filepath}\"";
Thanks for everyone's help, I tried all of these and unfortunately they didn't work which led me to believe that the issue wasn't what I originally thought.
It turns out that the files causing the Illegal characters in path all had carriage return characters at the end of the file name, after the file extension.
To resolve this I used the following code and now it works perfectly
FilePath = FilePath.TrimEnd('\r', '\n');
Thanks everyone for your help.
Try this:
String StringCorrectedFilePath = #""+ Filepath;

Reference Path On Another Server on Network C#

I am having an issue trying to reference a drive\path on another on the same network as my application.
string LocationPath = "\\servername\F$\FirstDirectory\SecondDirectory\filename.txt";
I would like to use streamreader to capture the contents of this file but can't seem to access it. This is how I reference the directory in file explorer, how can it be done in C#?
Thanks for any input!
You need to escape the backslash:
string LocationPath = "\\\\servername\\F$\\FirstDirectory\\SecondDirectory\\filename.txt";
Or use a verbatim string:
string LocationPath = #"\\servername\F$\FirstDirectory\SecondDirectory\filename.txt";

Strip Out Illegal Characters For Excel Sheet

I wrote a program to crawl website to get data and output to a excel sheet. The program is written in C# using Microsoft Visual Studio 2010.
For most of the time, I have no problem getting content from the website, parse it, and store data in excel.
However, once a will I'll run into issue, saying that there are illegal characters (such as ▶) that prevents outputting to excel file, which crashes the program.
I also went onto the website manually and found other illegal characters such as Ú.
I tried to do a .Replace() but the code can't seem to find those characters.
string htmlContent = getResponse(url); //get full html from given url
string newHtml = htmlContent.Replace("▶", "?").Replace("Ú", "?");
So my question is, is there a way to strip out all characters of those types from a html string? (the html of the web page) Below is the error message I got.
I tried Anthony and woz's solution and that didn't work...
See System.Text.Encoding.Convert
Example usage:
var htmlText = // get the text you're trying to convert.
var convertedText = System.Text.Encoding.ASCII.GetString(
System.Text.Encoding.Convert(
System.Text.Encoding.Unicode,
System.Text.Encoding.ASCII,
System.Text.Encoding.Unicode.GetBytes(htmlText)));
I tested this with the string ▶Hello World and it gave me ?Hello World.
You could try stripping all non-ASCII characters.
string htmlContent = getResponse(url);
string newHtml = Regex.Replace(htmlContent, #"[^\u0000-\u007F]", "?");
thank you for the replies and thanks for the help.
After couple more hours of googling I have found the solution to my question. The problem was that I had to "sanitize" my html string.
http://seattlesoftware.wordpress.com/2008/09/11/hexadecimal-value-0-is-an-invalid-character/
Above is the helpful article I found, which also provides code example.

Reading A Fixed Format Text File

Its been years since I have had to attempt to read a file using either Microsoft Text ODBC Driver or Microsoft Jet OLE DB 4.0 Provider.
So I have the following code
public void Example()
{
string CVS = Application.StartupPath;
string SQL = "SELECT * FROM [MyFile.txt]";
string Connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+CVS+";"+"Extended Properties='text;HDR=Yes;FMT=Fixed;";
OleDbDataAdapter OLE = new OleDbDataAdapter(SQL,Connection);
DataTable Table = new DataTable();
OLE.Fill(Table);
}
When I run the above code I get an "Unexpected Error", I know I am missing something, I am not sure what exactly.
Sources:
http://www.connectionstrings.com/textfile
http://www.connectionstrings.com/Providers/net-framework-data-provider-for-ole-db
http://www.aspdotnetcodes.com/Importing_CSV_Database_Schema.ini.aspx
Any direction would be appreciated.
Let us assume the Schema.ini file is correct.
Remove ' (just prior to 'text;) from the connection string.
In order to resolve the "Could not find installable ISAM", run the following command:
Regsvr32 c:\winnt\system32\mstext40.dll
* Make sure that file is in that folder first. And change WINNT to whatever your windows directory is.
I know this is not a real answer to your question, but i would really rethink about using this architecture to read in a file.
I would really prefer something like CSV Reader, cause it gives you much more power about how the data will be interpreted. Alternative you could also take a look into the FileHelpers.

Categories