CudoX
CudoX

Reputation: 993

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

Is there any way to prevent the conversion from TINYINT(1) to boolean? Or perhaps convert it to int instead?

My TINYINT(1) column can have data such as 0, 1, 2, 3. But it is being converted to False, True, True, True respectively.

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.

Upvotes: 1

Views: 2815

Answers (2)

Vivek Nuna
Vivek Nuna

Reputation: 1

TINYINT is basically 1 byte in SQL Server, and its .Net equivalent is 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

Upvotes: 1

Rahul
Rahul

Reputation: 77846

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

(byte)rdr["columnwithtinyintdatatype"];

Upvotes: 0

Related Questions