如何将byte []转换为varchar?

时间:2010-10-10 14:18:24

标签: c# mysql

SELECT Trainer.id AS idTrainer, surname, lastname, name,  
if(month = '', 'NO',month) as monthT, quarter, halfyear, year, id_Person, photo
FROM Trainer, Person
WHERE Trainer.id_Person = Person.id
phpmyadmin中的

if(month = '', 'NO',month) as monthT返回good或'no'或month

在C#中返回byte[],但如果month为空,则需要返回NO,否则返回month

我的C#代码如下所示:

string sql = @" SELECT Trainer.id AS idTrainer, surname, lastname, name,  if(month = '', 'NO',month) as monthT, quarter, halfyear, year, id_Person, photo
                FROM Trainer, Person
                WHERE Trainer.id_Person = Person.id";
using (MySqlConnection connection = ConnectToDataBase.GetConnection())
{
    MySqlCommand command = new MySqlCommand(sql, connection);
    MySqlDataAdapter adapter = new MySqlDataAdapter();

    connection.Open();
    adapter.SelectCommand = command;
}

2 个答案:

答案 0 :(得分:2)

在研究时我发现使用CAST存在VARCHAR的错误。解决方案是使用CHAR而不是VARCHAR。

证明链接:

Using Conversion Functions

Bug #34564 CAST does not accept varchar type

Cast Functions and Operators

答案 1 :(得分:0)

我不是MySql专家,但为什么你不尝试演员?

CAST(if(month = '', 'NO',month) AS VARCHAR) AS monthT

更多信息: http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html