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

本文关键字:一个 布尔值 int 我的 为什么 查询 mysql 返回 | 更新日期: 2023-09-27 17:51:18

我正在尝试从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自动将其解释为布尔值。所以我相信这与它是一个存储过程有关,而且它可能不是一个实际的存储值?

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

我错了,MySQL中没有c#的布尔类型,所以你只能使用NUMERIC类型,并将返回值作为c#的整数计算。

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