c#从数据库中检索BLOB文本



我有一个sqlite数据库,它有一个表(当前(和4列:

Name //TEXT
Quote //TEXT
ImageURL //TEXT, an online image
Entry //Text BLOB, I want to parse and store this as a List<Entry>

我已经制定了一些方法来创建文本文件、解析和存储数据以及删除文件。数据以以下格式存储:

$"{Index} {Time} {Date} {URL}" // "1 00:00.0 9/13/1985 www.stackoverflow.com"

这样,就更容易拆分这个字符串并将其存储在我的容器类Entry中。这些文件的大小平均为250字节,因此数据库内存很可能永远不会成为问题。

以下是连接到表中列数据的类:

public abstract class BaseClass
{
public abstract string Name {get; set;}
public abstract string ImageURL {get; set;}
public List<Entry> Records {get; private set;} //I already know .Add() is exposed, this problem has been fixed however.
...
}
public class DerivedClass : BaseClass
{
public override string Name {get; set;}
public override string ImageURL {get; set;}
public string Quote {get; set;}
...
}

在过去的几天里,我看到的解决方案涉及不同的语言和/或BLOB类型,并且没有一个同时使用Dapper、Sqlite和C#。

到目前为止,我已经返回了一个List<DerivedClass>,其中包含一个元素,该元素连接到我上面显示的派生类中的属性。除了BLOB之外的所有东西。

public static ClassType GetSingleRow(string primaryKey) where ClassType : BaseClass
{ 
using (IDbConnection connection = new SQLiteConnection(LoadConnectionString()))
{                
string sql = $"SELECT * FROM {typeof(ClassType).Name} WHERE Name = "{primaryKey}""; //probably vulnerable to sql injection but I'll worry about it later
var output = connection.Query<ClassType>(sql, new DynamicParameters());
//how would get a BLOB from this ienumearable?
return output.ToList()[0]
}
}

重申一下,如何使用Dapper从sqlite数据库中检索BLOB文本?我需要使用字节数组来检索它吗?如果是,我该怎么做?

好吧,就在我发布这个问题之前,我想,"为什么我不把另一处房产叫做";条目";所以Dapper可以帮我做剩下的工作。我最初把它做成了string,但在得到InvalidCastException后,我把它改成了byte[]

public class DerivedClass : BaseClass
{
public override string Name {get; set;}
public override string ImageURL {get; set;}
public string Quote {get; set;}
public byte[] Entry {get; set;}
...
}

然后在Main()中,我可以进行

Console.WriteLine(System.Text.Encoding.Default.GetString(DerivedClassObject.Entry));

通过我解析文本文件的方法,我可以很容易地将其存储在List<Entry>类中。

最新更新