This is an API that I have been looking for since I
started using C# with mysql, great :)
in MySqlCommand::ParseCommand, will there be a
support to use '?' for the param to be replaced? this
works in OdbcCommand
Will there be an UTF-8 support?
such as the feature,
useUnicode=true&characterEncoding=UTF-8, offered by
MySQL Connector/J (the official JDBC driver) My
application uses both C# and Java
basically, it ignores the system's character set, and
converts C# unicode strings into UTF-8 bytes, then
push the bytes into a VARCHAR column, and vice versa
I attempted the changes in MySql.cs, and
MySqlDataReader.cs:
public static string RealEscapeString(IntPtr db, string
str) {
return RealEscapeString(db,
System.Text.Encoding.UTF8.GetBytes(str));
}
public string GetString(int i) {
TryReadField(i);
ArrayList res = new ArrayList(256);
unsafe
{
byte ptr = (byte) _currentRow[i];
while (ptr != 0) res.Add(ptr++);
}
return System.Text.Encoding.UTF8.GetString((byte[])
res.ToArray(typeof(byte)), 0, res.Count);
}
The modified GetString() works ok, but the
RealEscapeString() doesn't. then I tried
command.Parameters.Add("@name",
MySqlDbType.VarCharBinary).Value =
System.Text.Encoding.UTF8.GetBytes(<chinese
characters, 中文, i.e. a 3-byte unicode>);
It gives:
MySqlSharp.Provider.MySqlException: Query failed
You have an error in your SQL syntax near ''銝剜' at line
1
at MySqlSharp.Provider.MySqlDataReader.NextResult
() in c:\tmp\mysqlsharp1.0be
ta1-src\src\provider\mysqldatareader.cs:line 191
at
MySqlSharp.Provider.MySqlCommand.ExecuteReader
(CommandBehavior cb) in c:\t
mp\mysqlsharp1.0beta1-
src\src\provider\mysqlcommand.cs:line 250
at Test.Test.Main(String[] args) in
c:\tmp\mysqlsharp1.0beta1-src\src\test\te
st.cs:line 52
the SQL in cmds[0] was SELECT * FROM Lookup
WHERE name='銝剜?', which is a correct UTF-8
VARCHAR encoding
Is that becoz the native mysql dll converts the encoded
string using the system's character set? In myOdbc, I
used
command.Parameters.Add(null,
OdbcType.VarBinary).Value =
System.Encoding.UTF8.GetBytes("中文");
and this gives the same output as the JDBC driver
Thanks
Thomas
Logged In: YES
user_id=409109
Looked at mysql's CAPI, we may need to use
mysql_escape_string for bytes
[DllImport("libmySQL.dll",
CharSet=System.Runtime.InteropServices.CharSet.Ansi,
EntryPoint="mysql_escape_string", ExactSpelling = true)]
private static extern int EscapeStringInternal(IntPtr to, IntPtr
from, uint length);
and the SQL error is due to the difference between
command's unicode and char* lengths.
MySql.RealQuery(_connection._mysql, _cmds
[_currentCommand], (uint)_cmds[_currentCommand].Length)
^^^
MySqlDataReader::NextResult
For bytes that may include 0 (end of string char), I suggest
that MySqlCommand::ParseCommand() to return byte[][] (or
a collection of byte[])
Thanks
Thomas