c#和MySQL从db和utf-8表中选择字符串

时间:2013-08-22 18:05:19

标签: c# mysql select utf-8

我用来创建与日期库的连接的代码

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;
            }
        }

无论姓名中的字符串是多少,它都可以读取它,但是当它是任何名称时程序告诉我找不到任何东西 这是表

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;

抱歉英语不好我希望能帮助解决我的问题

1 个答案:

答案 0 :(得分:1)

如果您的表格返回多行,您可以尝试使用ExecuteScalar()代替Execute()