litedb查看查询结果

时间:2017-04-13 14:17:22

标签: c# litedb

我想知道如何在窗体加载时从控制台中的LiteDB或多行文本框中以文本格式查看数据库的结果。这是我到目前为止所做的,但它并没有返回信息。

 private void DisplayData_Load(object sender, EventArgs e)
                    {
                        using (var db = new LiteDatabase(@"C:\Temp\MyData.db"))
                        {
                        // Get a collection (or create, if doesn't exist)
                            var col = db.GetCollection<DataBase>("data");

                        // Create your new customer instance
                           var results = col.FindAll();
                           Console.WriteLine(results);
                        }
                    }

1 个答案:

答案 0 :(得分:0)

我想我找到了答案的一个版本。此代码将数据显示到组合框,控制台输出或文本框的文本...

public class DataBase
    {
        [BsonId]
        public string GetSetVariable { get; set; }
    }

private void DisplayData_Load(object sender, EventArgs e)
{
    using (var db = new LiteDatabase(@"C:\Temp\MyData.db"))
    {
        // Get a collection (or create, if doesn't exist)
        var col = db.GetCollection<DataBase>("collection_name");

        // Enter data into the database
        var incomingData = new Database
        { 
            GetSetvariable = "This is output text."
        };

        // Create unique index in Name field
        col.EnsureIndex(x => x.GetSetVariable, true);

        // Insert new customer document (Id will be auto-incremented)
        col.Insert(incomingData);

        // Update a document inside a collection
        incomingData.GetSetVariable = "Updated Text Record";
        col.Update(incomingData);

        // Create a query
        var results = col.FindAll();

        // To display ALL columns of 'results' in a combo box.
        foreach (var finding in results)
        {
            var variable = finding.GetSetVariable;
            comboBox1.Items.Add(variable);
            Console.WriteLine(variable);
        }

        // To display one record of 'results' to a text box.
        var query = col.FindById(1);
        var variable = query.GetSetVariable;
        textBox1.Text = variable;
        Console.WriteLine(variable);
    }
}