Skip to content

Unable to read binary data stored in VARBINARY (n) type fields #1258

Description

@alexluozf

Hello,
Unable to read binary data stored in field BlockData VARBINARY (32,765) .

SortedDictionary<short, byte[]> result = [];
foreach (DataRow dataRow in dataTable.Rows)
{
if (dataRow["BlockNo"] is not short blockNo) { continue; }
object objVal = dataRow["BlockData"];
if(objVal is not byte[] blockData) { continue; }
//if (dataRow["BlockData"] is not byte[] blockData) { continue; }
result[blockNo] = blockData;
}
The value of objVal is always the string 'System. Byte []', the value of blockData is always the null.

Thanks a lot.
Alex

Activity

  1. alexluozf commented on Mar 12, 2026

    @alexluozf
    Author

    It's no problem if using FbDbDataReader.

    SortedDictionary<short, byte[]> result = [];
    while (reader.ReadAsync().Result)
    {
    short blockNo = reader.GetFieldValue("BlockNo");
    byte[] blockData = reader.GetFieldValue<byte[]>("BlockData");
    if (blockData != null)
    {
    result[blockNo] = blockData;
    }
    }

    Thanks a lot.
    Alex

  2. cincuranet commented on Mar 12, 2026

    @cincuranet
    Member

    Please provide self-contained runnable code.

  3. self-assigned this
    on Mar 12, 2026
  4. alexluozf commented on Mar 13, 2026

    @alexluozf
    Author

    Hello cincuranet,
    Please use my test project.
    Thanks a lot.
    Alex

    How to use.zip

    FbBinaryFieldAccess.zip

  5. alexluozf commented on Mar 14, 2026

    @alexluozf
    Author

    Hello cincuranet,

    Fixed this issue.
    method GetSystemType() in src\FirebirdSql.Data.FirebirdClient\Common\DbField.cs

    public Type GetSystemType()
    {
    	switch(DbDataType)
    	{
    		case DbDataType.Char:
    		case DbDataType.VarChar:
    		case DbDataType.Text:
    			{
    				if(_charset.IsOctetsCharset)
    				{
    					return TypeHelper.GetTypeFromDbDataType(DbDataType.Binary);
    				}
    				return TypeHelper.GetTypeFromDbDataType(DbDataType);
    			}
    		default:
    			return TypeHelper.GetTypeFromDbDataType(DbDataType);
    	}
    }
    

    Please review and update the code.
    Thanks a lot.
    Alex

    DbField.zip

  6. alexluozf commented on Mar 14, 2026

    @alexluozf
    Author

    Hello cincuranet,

    Should remove case DbDataType.Text.

    public Type GetSystemType()
    {
    switch(DbDataType)
    {
    case DbDataType.Char:
    case DbDataType.VarChar:
    {
    if(_charset.IsOctetsCharset)
    {
    return TypeHelper.GetTypeFromDbDataType(DbDataType.Binary);
    }
    return TypeHelper.GetTypeFromDbDataType(DbDataType);
    }
    default:
    return TypeHelper.GetTypeFromDbDataType(DbDataType);
    }
    }

    DbField.zip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions