TINYINT and Byte

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for MySQL
Post Reply
Guest

TINYINT and Byte

Post by Guest » Tue 03 Jan 2006 09:54

I don't understand why I read a TINYINT with Reader.GetByte I have an error :
Exception runtime levée : System.InvalidCastException - La valeur des données n'a pas pu être convertie pour des raisons autres que l'incompatibilité de signe ou le dépassement de données. Par exemple, les données étaient endommagées dans le magasin de données mais la ligne pouvait toujours être extraite.
I use this code :

Code: Select all

        Me.UniConnection1.Open()
        Try
            Dim myReader As UniDataReader = Me.UniCommand1.ExecuteReader()
            ' Always call Read before accessing data.
            While myReader.Read()
                Console.WriteLine(myReader.GetString(0) & ", " _
                    & myReader.GetInt16(1) & ", " _
                    & myReader.GetByte(2))
            End While
            ' always call Close when done reading.
            myReader.Close()
            ' Close the connection when done with it.
        Finally
            Me.UniConnection1.Close()
        End Try
And my query is :

Code: Select all

SELECT `NomColonne`, `Taille`, `Visible` FROM `t_pref_datagrid_com` ORDER BY `NumOrdreColonne`
And my table MySQL is :

Code: Select all

CREATE TABLE `t_pref_datagrid_com` (
  `Id` int(11) NOT NULL auto_increment,
  `IdUser` int(11) NOT NULL default '0',
  `NumDatagrid` smallint(6) NOT NULL default '0',
  `NomColonne` varchar(33) NOT NULL default '',
  `NumOrdreColonne` tinyint(4) NOT NULL default '0',
  `Taille` smallint(6) NOT NULL default '0',
  `Visible` tinyint(4) NOT NULL default '0',
  PRIMARY KEY  (`Id`)
) TYPE=MyISAM;
And I use MySQL 4.0.16 and MySQLDirect .NET 3.20.7.0.

The byte and tinyint is not the same storage required ??

Thank you for your help.

Serious

Post by Serious » Tue 03 Jan 2006 10:32

Signed tinyint columns are mapped to System.Int16 type (SByte type is not CLS-compliant).
Use MySqlDataReader.GetSchemaTable method (or MySqlDataAdapter designer) to recognize .NET Framework types returned by your queries.

daner06
Posts: 6
Joined: Tue 03 Jan 2006 09:41

Post by daner06 » Tue 03 Jan 2006 11:26

ok, but my field is not signed...
`Visible` tinyint(4) NOT NULL default '0',
I have this problem because I use UniDirect and I do access to my databases MySQL and MS Access and my Access field is byte...

Do you have a solution ? :?

Serious

Post by Serious » Tue 03 Jan 2006 11:41

We cannot map type with range -128..128 to type with range 0..255.
Do you have a solution ? Confused
Solution is to use Int16 type or use implicit type cast.
Moreover, looks like your MySQL database schema does not correspond to Access one.

daner06
Posts: 6
Joined: Tue 03 Jan 2006 09:41

Post by daner06 » Tue 03 Jan 2006 12:53

Thank you for your reponse. It's good.

My old driver MySQL Connector/Net use GetByte for read tinyint fields, but your driver is more right.

:)

Serious

Post by Serious » Tue 03 Jan 2006 13:11

Connector/NET provider uses explicit cast from SByte to Byte in MySqlDataReader.GetByte method. Negative values are lost after this conversion.

Post Reply