为什么我的mysql查询总是返回一个int值而不是一个布尔值?

时间:2013-01-11 16:58:35

标签: c# mysql

我试图从C#中读取以下存储过程。

DECLARE IsTrue boolean;
DECLARE IsFalse boolean;
set IsTrue = true;
set IsFalse = false;
SELECT  
     stuff.ID1
    ,stuff.ID2
    ,stuff.ID3
    ,stuff.ID4
      ,CASE  
            WHEN stuff1.ID1 IS NOT NULL THEN IsTrue
            WHEN stuff1.ID1 IS NULL THEN IsFalse
            END AS 'stuff1Column'   
      ,CASE   
            WHEN stuff2.ID1 IS NOT NULL THEN IsTrue
            WHEN stuff2.ID1 IS NULL THEN IsFalse
            END AS 'stuff2Column'   
      FROM myStuff.stuff
      LEFT JOIN myStuff.stuff1 ON stuff.ID1 = myStuff.stuff1.ID1
      LEFT JOIN myStuff.stuff2 ON stuff2.ID1 = myStuff.stuff2.ID1
      ORDER BY stuff.ID1 DESC;

基本上在C#中我抛出以下异常。

Object of type 'System.Int32' cannot be converted to type 'System.Boolean'.

即使我指定返回一个布尔值,它也会给我一个Int。我也尝试过使用tinyint(1),它仍然没有用。

这是班级:

public class Stuff {
        private int _ID1;
        private int _ID2;
        private int _ID3;
        private int _ID4;
        private bool _stuff1Column;
        private bool _stuff2Column;

    public int ID1 {
        get { return _ID1; }
        set { _ID1 = value; }
    }
    public int ID2 {
        get { return _ID2; }
        set { _ID2 = value; }
    }
    public int ID3 {
        get { return _ID3; }
        set { _ID3 = value; }
    }
    public int ID4 {
        get { return _ID4; }
        set { _ID4 = value; }
    }
    public bool Stuff1Column {  
        get { return _stuff1Column; }
    set { _stuff1Column = value; }
    }
    public bool Stuff2Column {  
        get { return _stuff2Column; }
        set { _stuff2Column = value; }
    }   
}

编辑1

我的班级正在尝试将stuff1Columnstuff2Column作为bool值读取,因为这就是属性。

编辑2

这是读取它的C#代码。

public static List<T> Read<T>(T data, string procedure) {
    List<T> collection = new List<T>();
    PropertyInfo[] properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
    List<PropertyInfo> propertiesToSearch = new List<PropertyInfo>();
    foreach (PropertyInfo prop in properties) {
        if (prop.GetCustomAttributes(false).Any(x => x.GetType() == typeof(DataParameter)))
            propertiesToSearch.Add(prop);
    }
    MySqlConnection connection = new MySqlConnection(Properties.Settings.Default.MySqlConnection);
    MySqlCommand command = new MySqlCommand(procedure, connection);
    command.CommandType = CommandType.StoredProcedure;
    foreach (PropertyInfo property in propertiesToSearch) {
        var parameterName = "@" + property.Name;
        var value = property.GetValue(data, null);
        command.Parameters.AddWithValue(parameterName, value);
    }
    connection.Open();
    MySqlDataReader reader = command.ExecuteReader();
    while (reader.Read()) {
        T item = Activator.CreateInstance<T>();
        foreach (PropertyInfo property in propertiesToSearch) {
            if (reader[property.Name] is MySql.Data.Types.MySqlDateTime) {
                property.SetValue(item, (DateTime)(MySql.Data.Types.MySqlDateTime)reader[property.Name], null);
            } else {
                Type test = reader[property.Name].GetType();
                property.SetValue(item, reader[property.Name], null);
            }
        }
        collection.Add(item);
    }
    reader.Close();
    connection.Close();
    return collection;
}

编辑3

我有另一个存储了tinyint(1)值的表,而MySqlDataReader会自动将其解释为布尔值。所以我认为它与存储过程有关,而且它可能不是实际的存储值这一事实?

1 个答案:

答案 0 :(得分:2)

我错了,在MySQL中没有相当于C#的布尔类型,所以你只能使用NUMERIC类型并将返回的值作为C#中的ntegers进行评估。

因此,保持BOOLEAN类型并修改您的C#代码以评估返回的BOOLEAN值(因此0表示False,1表示True)。