简体   繁体   English

C# - MySQL 防止 TINYINT(1) 到 Boolean 的转换

[英]C# - MySQL Prevent TINYINT(1) to Boolean Conversion

Is there any way to prevent the conversion from TINYINT(1) to boolean?有什么办法可以防止从 TINYINT(1) 到 boolean 的转换吗? Or perhaps convert it to int instead?或者也许将其转换为 int ?

My TINYINT(1) column can have data such as 0, 1, 2, 3. But it is being converted to False, True, True, True respectively.我的 TINYINT(1) 列可以包含 0、1、2、3 等数据。但它分别被转换为 False、True、True、True。

Here is the code that I currently use:这是我目前使用的代码:

using (Cmd = new MySqlCommand())
{
    Cmd.Connection = Conn;
    SetQuery();

    using (var dt = new DataTable())
    {
        dt.Load(Cmd.ExecuteReader());
        ObjectList = dt.AsEnumerable().ToArray();=
    }

    Parse();

    return ObjectList != null;
}

Hope you can help me with this problem.希望你能帮我解决这个问题。

Thanks in advance.提前致谢。

TINYINT is basically 1 byte in SQL Server, and its.Net equivalent is byte . TINYINT在 SQL Server 中基本上是 1 byte ,它的 .Net 等价物是byte You can do this in multiple ways, have a look.您可以通过多种方式执行此操作,请看一下。

int x = (int)(byte) reader["column"];
int x = (byte) reader["column"];
int x = reader.GetByte(column);

Refere SQL Server Data Type Mappings参考SQL 服务器数据类型映射

Though your posted code nowhere shows the actual conversion from TINYINT(1) to boolean as you mentioned but you can try casting it to byte like尽管您发布的代码无处显示您提到的从TINYINT(1)boolean的实际转换,但您可以尝试将其强制转换为byte

(byte)rdr["columnwithtinyintdatatype"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM