1

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!

Community
  • 1
  • 1
Jessica
  • 11
  • 1
  • Your quote marks don't look right. Use the single quote next to the Enter key instead of the one next to 1. – lucrativelucas Feb 27 '15 at 14:31
  • These are the symbols to mark characters, not strings and you will get a syntax error when you use the quotes next to enter to wrap more than one character. – Jessica Feb 27 '15 at 15:16

0 Answers0