使用Blob在SQLite中存储Byte数组

对于我的应用程序,我正在尝试在我的SQLite应用程序中存储一个字节数组,我用这种方式填充我的SQLite数据库:

public bool InsertMessage() { //create string SQl and fill it with the SQLite query for inserting a message. string SQL = "Insert into ClockMessages(InsertDateTime, SendDateTime, Data) Values('"+ insertdatetime + "', null, '" + data + "');"; List pars = new List(); pars.Add(new SQLiteParameter("InsertDateTime", insertdatetime)); pars.Add(new SQLiteParameter("Data", data)); //send the SQl query and it's parameters to the SQLiteDatabase class return SQLiteDatabase.ExecuteNonQuery(SQL, pars); } 

其中InsertDateTime是DateTime.UtcNow,Data是字节数组。 这样的结果导致一个字段数据包含:System.Byte [],我不能只显示该字段中的实际字节数组,而不显示字符串? 我到处使用Byte []类型来填充数据库,所以我不会将它转换为字符串。 我想在数据字段中看到类似的内容:01,54,32,23,11,02,53。数据库中数据字段的DataType是:BLOB。

任何人都可以帮我实现这个目标吗?

您应该在语句中使用参数:

 string SQL = "INSERT INTO ClockMessages (InsertDateTime, SendDateTime, Data) VALUES (@InsertDateTime, NULL, @Data)"; 

其他一切似乎都是正确的。

你现在拥有的,在insertdatetimedata上调用ToString()方法, insertdatetime结果连接到sql语句。 实际上你没有在声明中使用任何参数。

使用带有DBType参数的DBType

 var dataParameter = new SQLiteParameter("Data", DbType.Binary) { Value = data }; pars.Add(dataParameter);