Last active
July 3, 2018 12:59
-
-
Save JeffHenson/17b95e5b12f5428ebe26618a06901d97 to your computer and use it in GitHub Desktop.
NuoDb Binary Column Repro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using NuoDb.Data.Client; | |
namespace NuoDbBinaryRepro | |
{ | |
internal class Program | |
{ | |
public static void Main() | |
{ | |
using (var con = new NuoDbConnection("Server=localhost;Database=Test;User=dba;Password=dba;Schema=test;")) | |
{ | |
con.Open(); | |
using (var cmd = con.CreateCommand()) | |
{ | |
cmd.CommandText = "DROP TABLE IF EXISTS BinaryTest;"; | |
cmd.ExecuteNonQuery(); | |
} | |
using (var cmd = con.CreateCommand()) | |
{ | |
cmd.CommandText = "CREATE TABLE BinaryTest (BinaryColumn VARBINARY(100));"; | |
cmd.ExecuteNonQuery(); | |
} | |
using (var cmd = con.CreateCommand()) | |
{ | |
cmd.CommandText = "INSERT INTO BinaryTest VALUES (?);"; | |
cmd.Parameters.Add(Encoding.UTF8.GetBytes("Hello world!")); | |
cmd.ExecuteNonQuery(); | |
} | |
using (var cmd = con.CreateCommand()) | |
{ | |
cmd.CommandText = "SELECT BinaryColumn FROM BinaryTest;"; | |
using (var dr = cmd.ExecuteReader()) | |
{ | |
if (dr.Read()) | |
{ | |
var ordinal = dr.GetOrdinal("BinaryColumn"); | |
// This call is requried to for the declaredColumnTypes variable to be populated. | |
// In my testing, this seems to happen automatically seemingly randomly. | |
var type = dr.GetFieldType(ordinal); | |
// This call errors out with: System.InvalidCastException: Object must implement IConvertible. | |
var bytes = (byte[]) dr["BinaryColumn"]; | |
Console.WriteLine(Encoding.UTF8.GetString(bytes)); | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment