0

I would like to know how to see the results from a database in text format from LiteDB in the console or a multi line text box when the form loads. This is what I have so far, but it doesn't return the information.

 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);
                        }
                    }
ACopeLan
  • 154
  • 1
  • 8
  • What is the problem you are encountering with the code? Your `results` should have a collection of `DataBase` objects which you can iterate into the console or add to a textbox. – Dustin Kingen Apr 13 '17 at 14:51
  • Yes, there is information contained in the database. I don't deal with databases often so I would like to know how to iterate through the code to display all the contents of the database to the console, or to a text box. – ACopeLan Apr 13 '17 at 14:57

1 Answers1

0

I think I found a version of the answer. This code displays the data to a combo box, console output, or as text for a text box...

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);
    }
}
ACopeLan
  • 154
  • 1
  • 8