Example Netduino Application

The example included with the source is for use with Secret Lab's Netduino (http://netduino.com), and also requires MicroLinq (http://microlinq.codeplex.com/). After creating a new Netduino application and importing the required references, define your table's model with a simple class:
    [Serializable]
    public class Product
    {
        public int Id;      
        public string Name;
        public float Cost;

        public Product(int id, string name, float cost)
        {
            this.Id = id;
            this.Name = name;
            this.Cost = cost;
        }

        public override string ToString()
        {
            return "Id:" + this.Id.ToString() + ", Name: " + this.Name + ", Cost: $" + this.Cost;
        }
    }

In MuDB, public fields correspond to table columns. Notice that the class is also marked Serializable. To start the process, create a new instance of MuDB and insert a new table into it by passing the type of our model (Product):
    Database db = new Database(null);
    var Products = db.Insert(typeof(Product));

Inserting records into the table:
    Products.Insert(new Product(1, "Apple", 1.50f));
    Products.Insert(new Product(2, "Orange", 1.75f));
    Products.Insert(new Product(3, "Banana", 1.30f));

Note that attempting to create two tables of the same type, or trying to insert a different type will result in a DatabaseException. After inserting our data, we can query it using MicroLinq:
    var product = Products.Data.FirstOrDefault(p => ((Product)p).Name == "Apple");
    Debug.Print(product.ToString()); 

Which yields:
Id:1, Name: Apple, Cost: $1.5

Last edited Mar 5, 2011 at 1:19 PM by Solidus117, version 3

Comments

No comments yet.