4

对于我的应用程序,我试图在我的 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<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);
    }

其中 InsertDateTime 是 DateTime.UtcNow 而 Data 是一个字节数组。此结果的输出导致字段数据包含: System.Byte [],我不能只在该字段中显示真正的字节数组,而不显示字符串吗?我在任何地方都使用 Byte[] 类型来填充数据库,所以我不会将其转换为字符串。我想在Data字段中看到这样的东西:01、54、32、23、11、02、53。数据库中Data字段的DataType是:BLOB。

谁能帮我实现这一目标?

4

2 回答 2

3

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

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

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

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

于 2013-02-25T12:17:47.907 回答
2

使用带DBType参数的 SQLiteParameter 重载:

var dataParameter = new SQLiteParameter("Data", DbType.Binary) { Value = data };
pars.Add(dataParameter);
于 2013-02-22T13:47:08.043 回答