将byte[]存储在SQLite数据库中,然后从SQLite数据库选择byte[]

本文关键字:数据库 SQLite byte 然后 选择 存储 | 更新日期: 2023-09-27 18:00:50

我有一个SQLiteDatabas,我想把我的byte[]存储在一个名为"Data"的字段中,我现在使用的Datetype被称为:Blob,在SQLiteDatabase中存储byte数组的方法如下:

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<SQLiteParameter> pars = new List<SQLiteParameter>();
    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);
}

我的SQLiteDatabase中的字段Data现在包含以下文本:System.Byte[]

我想把实际的byte[]存储在数据库中,如何实现这一点?"数据"字段是否需要另一个DateType?

将byte[]存储在SQLite数据库中,然后从SQLite数据库选择byte[]

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

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

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

我会将字节数组转换为base64字符串,并将其存储在nvarchar(max(字段中

转换.ToBase64String(byte[](

http://msdn.microsoft.com/en-us/library/dhx0d524.aspx

并通过byte[]=Convert.FromBase64String 将其还原