在SQLite表中读取BLOB到字节数组

时间:2014-11-13 17:00:07

标签: c# sqlite blob

我想将一个blob读取到一个字节数组,并在cmd中显示当前的字节数组。

这是我目前的代码:

string connector = @"data source=C:\testfile; Version=3;";

SQLiteConnection connection = new SQLiteConnection(connector);
SQLiteCommand command = new SQLiteCommand(connection);

connection.Open();

command.CommandText = "SELECT file FROM users";

SQLiteDataReader reader = command.ExecuteReader();

while (reader.Read())
{
    int i = 0;

    byte[] bytBLOB = new byte[reader.GetBytes(0, 0, null, 0, int.MaxValue) - 1];
    reader.GetBytes(0, 0, bytBLOB, 0, bytBLOB.Length);
    Console.WriteLine(bytBLOB.ToString());

    i++;    
}

目前我的代码不起作用。它打印出两行(我在"文件"中有两个项目):

  

System.Byte []
  System.Byte []

如何使我的代码正常工作。在c#中读取所有相关帖子到blob并不能帮助我解决这个问题。我使用了这个帮助:blogpost

1 个答案:

答案 0 :(得分:1)

.ToString()byte[]上没有像您期望的那样工作。你需要这样的东西:

Console.WriteLine( Encoding.Default.GetString( bytBLOB ) ) ;

假设blob中的字节表示默认编码中的可打印字符。如果他们是随机数据,您需要逐字节转储它们。

相关问题