数据库Connection和字段的字符集都采用utf-8.
开始使用存储过程插入数据,代码大约如下
MySqlCommand cmd = new MySqlCommand("apend_testproject",
new MySqlConnection(cConnectMySqlHelp.GetConnectStr().ConnectionString));
commonfunc.logfile(cmd.Connection.ConnectionString);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new MySqlParameter("_PM", _PM));
cmd.Parameters.Add(new MySqlParameter("_PUBLISH_TIME", _PUBLISH_TIME));
cmd.Parameters.Add(new MySqlParameter("_PRODUCT_LINE", _PRODUCT_LINE));
cmd.Parameters.Add(new MySqlParameter("_NAME", HttpUtility.UrlDecode(_NAME)));
cmd.Parameters.Add(new MySqlParameter("_PROGRESS", 100));
cmd.Parameters.Add(new MySqlParameter("_TEST_SUB_TIME", _TEST_SUB_TIME));
cmd.Parameters.Add(new MySqlParameter("_PROJECT_TYPE", _PROJECT_TYPE));
结果数据库表中显示乱码,很郁闷,以为是字符集编码的问题,去网上找了些资料说可以如下操作,意思是
在connectstring里边加入字符集说明utf-8
public static MySqlConnectionStringBuilder GetConnectStr()
{
MySqlConnectionStringBuilder connBuilder =
new MySqlConnectionStringBuilder();
connBuilder.Add("Database", "testplat");
connBuilder.Add("Data Source", "10.2.226.20");
connBuilder.Add("User Id", "root");
connBuilder.Add("Password", "");
//connBuilder.Add("charset", "utf8");
return connBuilder;
}
结果还是错误。
实在没有办法,根据测试直觉,是不是由于存储过程是在数据库端,所以,,,,
那么直接写Sql调用试试,
代码如下
MySqlCommand cmd = new MySqlCommand();
cmd.CommandType = CommandType.Text;
cmd.Connection = new MySqlConnection(cConnectMySqlHelp.GetConnectStr().ConnectionString);
cmd.CommandText = "insert `testplat`.`TESTPROJECT` (PM,PUBLISH_TIME,PRODUCT_LINE,NAME,PROGRESS,TEST_SUB_TIME,PROJECT_TYPE) "+
"values('" + _PM + "','" + _PUBLISH_TIME + "','" + _PRODUCT_LINE + "','" + _NAME + "'," + 100 + ",'" + _TEST_SUB_TIME + "'," + _PROJECT_TYPE + ")";
中文不再显示乱码,于是可以断言是存储过程的问题
为了验证,直接到Mysql客户端工具中
执行存储过程:
CALL apend_testproject('elbert', '2009-11-30', 0, '木牛流马',2, '2009-10-30', 2);
报:
Incorrect string value: '/xE6/x9C/xA8/xE7/x89/x9B...' for column '_NAME' at row 1
至此可以断言MySql有b ug;
虽然经过验证,但是毕竟不是mysql方面专家,所以标题加上疑似!
附mysql版本
mysql Ver 14.12 Distrib 5.0.81, for pc-linux-gnu (i686) using readline 5.1