关注开源代码的实际应用
有两种方法可以实现:
①、通过SQLiteParameter
- static void TestSqliteBinary()
- {
- using (SQLiteConnection cnn = new SQLiteConnection("data source=temp.db"))
- {
- cnn.Open();
- using (SQLiteCommand cmd = cnn.CreateCommand())
- {
- //cmd.CommandText = "Create Table test(data Image)";
- //cmd.ExecuteNonQuery();
- cmd.CommandText = "insert into test values(@data)";
- SQLiteParameter para = new SQLiteParameter("@data",DbType.Binary);
- string file = @"info.zip";
- FileStream fs = new FileStream(file, FileMode.Open);
- StreamUtil su = new StreamUtil();
- byte[] buffer = su.StreamToBytes(fs);
- fs.Close();
- para.Value = buffer;
- cmd.Parameters.Add(para);
- cmd.ExecuteNonQuery();
- }
- }
- }
②、通过16进制字符编码:
- INSERT INTO Foo (blob) VALUES(X'BADF00D')
In the above example, the binary data is quoted as hex, but inserted into the database as actual binary characters. Data is retrieved as a byte[] array.