首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的mysql查询总是返回int值而不是布尔值?

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

Stack Overflow用户
提问于 2013-01-11 16:58:35
回答 1查看 1.1K关注 0票数 1

我正在尝试从C#中读取以下存储过程。

代码语言:javascript
复制
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#中,我抛出以下异常。

代码语言:javascript
复制
Object of type 'System.Int32' cannot be converted to type 'System.Boolean'.

即使我指定返回一个布尔值,它还是给了我一个Int。我也尝试过使用tinyint(1)来解决这个问题,但是它仍然没有起作用。

这是一堂课:

代码语言:javascript
复制
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

我的类正在尝试以bool值的形式读取stuff1Column和stuff2Column,因为属性就是这样的。

编辑2

下面是读取它的C#代码。

代码语言:javascript
复制
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自动将其解释为一个布尔值。所以,我认为这与它是一个存储过程有关,而事实上它不是一个实际的存储值,可能吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-11 17:01:13

我错了,在MySQL中没有C#的布尔类型,所以您只能使用数字类型并在C#中将返回的值计算为ntegers。

因此,保持布尔类型并修改C#代码,以求返回的布尔值( False为0,True为1)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14282980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档