故障问题收集:
1. Client does not support authentication protocol requested by server
场景:MySQL Server 5.0,ByteFX.Data 0.76,执行MySqlConnection.Open()时报错。
原因:MySQL后来的版本跟之前的版本密码加密的Hash算法或Code不一样,而ByteFX.Data 0.76这样的Provider或者是MySQL的ODBC驱动等,在新的Server版本之下用的仍然是老的加密方法,造成MySQL登陆故障。
解决:SET PASSWORD FOR root@localhost = OLD_PASSWORD('New Password');
2. Data too long for column 'xx' in row 1
场景:MySQL Server 5.0,ByteFX.Data 0.76,执行Insert语句,字段值中有中文,报上面的错误
原因:MySQL 5.0字符集处理的问题
解决:
将MySQL安装目录下的my.ini文件中的字符集配置如下(修改完之后,最好重启一下MySQL服务):
[mysql]
default-character-set=gbk
[mysqld]
default-character-set=gbk
将表和字段的字符集属性都改成gbk。
在每一次MySqlCommand Execute语句时,如果SQL语句或者参数中包含中文,或者是返回的记录集中有中文(指Select的查询语句),就在这个Execute的SQL语句之前加一个SET NAMES GBK;。
开发示例代码:
1. 参数化方式 使用ByteFX.Data完整示例

使用参数化方式


{
string insert = "set names gbk;insert into TblItem (OID,ItemNo,ItemName,ItemSpec,ItemUOM) values(@OID,@ItemNo,@ItemName,@ItemSpec,@ItemUOM)";
oMsqlCon.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = oMsqlCon;
cmd.CommandText = insert;

MySqlParameter param1 = new MySqlParameter();
MySqlParameter param2 = new MySqlParameter();
MySqlParameter param3 = new MySqlParameter();
MySqlParameter param4 = new MySqlParameter();
MySqlParameter param5 = new MySqlParameter();
param1.MySqlDbType = MySqlDbType.VarChar;
param2.MySqlDbType = MySqlDbType.VarChar;
param3.MySqlDbType = MySqlDbType.VarChar;
param4.MySqlDbType = MySqlDbType.VarChar;
param5.MySqlDbType = MySqlDbType.VarChar;
param1.Size = 36;
param2.Size = 50;
param3.Size = 100;
param4.Size = 100;
param5.Size = 10;
param1.ParameterName = "@OID";
param2.ParameterName = "@ItemNo";
param3.ParameterName = "@ItemName";
param4.ParameterName = "@ItemSpec";
param5.ParameterName = "@ItemUOM";

foreach (DataRow row in ds.Tables[0].Rows)

{
param1.Value = row["OID"].ToString();
param2.Value = row["ItemNo"].ToString();
param3.Value = row["ItemName"].ToString();
param4.Value = row["ItemSpec"].ToString();
param5.Value = row["ItemUOM"].ToString();
cmd.Parameters.Add(param1);
cmd.Parameters.Add(param2);
cmd.Parameters.Add(param3);
cmd.Parameters.Add(param4);
cmd.Parameters.Add(param5);

cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}

oMsqlCon.Close();
}
2. 使用Batch Insert方式(执行效率跟参数化方式基本一样)

使用Batch Insert方式


{
string sMysqlCon = "Data Source=localhost;Database=test;User ID=root;Password=25154683";
MySqlConnection oMsqlCon = new MySqlConnection(sMysqlCon);

string insert = "";
oMsqlCon.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = oMsqlCon;

int k = 1;
insert = "set names gbk;";
string[] sql = new string[(ds.Tables[0].Rows.Count + 99) / 100];
foreach (DataRow row in ds.Tables[0].Rows)

{
insert = insert + "insert into item (OID,ItemNo,ItemName,ItemSpec,ItemUOM) values(";
insert = insert + "'" + row["OID"].ToString() + "',";
insert = insert + "'" + row["ItemNo"].ToString().Replace("'","''") + "',";
insert = insert + "'" + row["ItemName"].ToString().Replace("'","''") + "',";
insert = insert + "'" + row["ItemSpec"].ToString().Replace("'","''") + "',";
insert = insert + "'" + row["ItemUOM"].ToString().Replace("'","''") + "');";

k++;

if (k % 100 == 0)

{
sql[((k + 99) / 100)-1] = insert;
insert = "set names gbk;";
}
}
if (k % 100 != 0)

{
sql[((k + 99) / 100)-1] = insert;
}

for (int i = 0; i < sql.Length; i++)

{
cmd.CommandText = sql[i];
cmd.ExecuteNonQuery();
}

oMsqlCon.Close();
}