I am working at a c# database program which is managing logfiles. But now I got a problem: I created the new sql query-string which is searching for a filepath that contains backslashes and send it to the database (I am using Microsoft MySQL 5.7) but I get no results.
In the c# program, of course I have to write each \ as \\ which shouldn't be a problem. What I am wondering about is: When I use the debugger to see the value of my c#-search-string and copy it directly into the query field of Microsoft MySQL workbench (for example "SELECT * FROM tblfiles
WHERE file_path
='\\myserver.com\database\testTEST-MAREN\2.9.1.4\2011_4_29\filename.ext';"), I get the correct result.
So I assume that it could be an encoding problem. I found this thread here C# Convert string from UTF-8 to ISO-8859-1 (Latin1) H which seems to be a solution because SQL is encoded in latin1 and c# in utf8 (is this always correct?). So I implemented this short converter-function shown in the answer from Nathan Baulch. But the result is the same, if I send the query out of my c# program, the result is 0 and if I send it out of the workbench, I get the correct result.
This is how I use my queries in c#:
static string convertEncoding(string Message)
{
Encoding iso = Encoding.GetEncoding("ISO-8859-1");
Encoding utf8 = Encoding.UTF8;
byte[] utfBytes = utf8.GetBytes(Message);
byte[] isoBytes = Encoding.Convert(utf8, iso, utfBytes);
string msg = iso.GetString(isoBytes);
return msg;
}
string strSQL_get = "SELECT * FROM `tblfile_based_data` WHERE `file_path`='" + blf_path.Replace("/", "\\") + blf_name + "';";
strSQL_get = convertEncoding(strSQL_get);
var sqlConn = SqlDBlib.SqlConnection(connection_string);
//open connection to sqlite database
sqlConn.Open();
try
{
//get the logfile's id from tblfile_based_data
var sql_delCmd = DAS_Testdata_Management.SqlLibrary.SqlDataAdapter(strSQL_get, sqlConn);
DataTable tbl_FileId = new DataTable();
sql_delCmd.Fill(tbl_FileId);
//this should give only 1 result
string fileID = "";
if (tbl_FileId.Rows.Count == 1)
{
fileID = tbl_FileId.Rows[0].ItemArray[0].ToString();
}
}
catch (Exception tmpE)
{
}
I really would appreciate an answer that helps me solving this problem!