c#和MySQL从db和utf-8表中选择字符串
本文关键字:选择 字符串 db MySQL utf-8 | 更新日期: 2023-09-27 18:28:36
我用来创建带有数据库的连接的代码
private static string MySqlUsername, MySqlPassword, MySqlDatabase, MySqlHost;
private static string ConnectionString;
public static void CreateConnection(string user, string password, string database, string host)
{
MySqlUsername = user;
MySqlHost = host;
MySqlPassword = password;
MySqlDatabase = database;
ConnectionString = "Server=" + MySqlHost + ";Database='" + MySqlDatabase + "';Username='" + MySqlUsername + "';Password='" + MySqlPassword + "';Pooling=true; Max Pool Size = 160000; Min Pool Size = 0;CHARSET=utf8";
}
public static MySql.Data.MySqlClient.MySqlConnection MySqlConnection
{
get
{
MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection();
conn.ConnectionString = ConnectionString;
return conn;
}
}
我的问题是,当我用代码从utf-8数据库中选择字符串时
public void accf()
{
MySqlCommand cmd = new MySqlCommand(MySqlCommandType.SELECT);
cmd.Select("med").Where("Name", MName.Text).Or("UID", MUID.Text).Execute();
MySqlReader r = new MySqlReader(cmd);
if (r.Read())
{
CharName.Text = r.ReadString("Name");
}
else
{
fnf.ForeColor = Color.Red;
fnf.Text = "can't found anything";
fnf.Visible = true;
}
}
无论Name处的字符串是数字,它都可以读取,但当它是任意名称时,程序告诉我什么都找不到这是表格
DROP TABLE IF EXISTS `med`;
CREATE TABLE `med` (
`Name` varchar(16) CHARACTER SET utf8 NOT NULL,
`UID` bigint(18) unsigned NOT NULL,
`Substance` varchar(45) CHARACTER SET utf8 NOT NULL,
`Expiry` date DEFAULT NULL,
`Type` tinyint(5) unsigned DEFAULT '10',
`Note` varchar(18) DEFAULT NULL,
PRIMARY KEY (`UID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
很抱歉英语不好,我希望这能帮助解决我的问题
如果表返回多行,可以尝试ExecuteScalar()
而不是Execute()
。