1

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.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
CudoX
  • 985
  • 2
  • 19
  • 31
  • where is TINYINT? – M.kazem Akhgary Nov 13 '16 at 19:17
  • @M.kazemAkhgary, he means one of the table column is `TINYINT(1)` – Rahul Nov 13 '16 at 19:18
  • I don't know, but as I read posts it seems they are kinda same. if tinyint is just 1 byte you maybe able to retrieve that using unsafe code? take a look at this answer http://stackoverflow.com/a/4980975/4767498 – M.kazem Akhgary Nov 13 '16 at 19:22
  • Possible duplicate of [How to retrieve an int value rather than a boolean from a TINYINT(1) column?](http://stackoverflow.com/questions/9728994/how-to-retrieve-an-int-value-rather-than-a-boolean-from-a-tinyint1-column) – Luke Woodward Nov 13 '16 at 21:04

2 Answers2

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

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
0

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"];
Rahul
  • 76,197
  • 13
  • 71
  • 125