簡體   English   中英

c# sqlite - 從數據庫中檢索 BLOB 文本

[英]c# sqlite - Retrieve BLOB text from database

我有一個帶有單個表(當前)和 4 列的 sqlite 數據庫:

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 文本? 我需要使用字節數組來檢索它嗎? 如果是這樣,我該怎么做?

好吧,就在我要發布這個問題之前,我想,“為什么我不創建另一個名為“Entry”的屬性,以便 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>類中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM