Below is a short example of using the MicroORM code:
            Utilities.SQL.MicroORM.MicroORM.Database("YourConnectionString");                       //Sets the default connection string
            Utilities.SQL.MicroORM.MicroORM.Database("YourConnectionString","Database2");           //Sets the connection string. Saving it to Database2.
            Utilities.SQL.MicroORM.MicroORM.Map<MyClass>("Table to map to", "ID")                   //Sets the table name, and tells the mapping that the ID field is the primary key
                .Map(x => x.ID)                                                                     //Maps the ID field
                .Map(x => x.Name, Length: 100)                                                      //Maps the name field, telling the mapping it uses a length of 100
                .Map(x => x.Something, DatabasePropertyName: "someThing", DefaultValue: 10.0f);     //Maps the something field, sets the field name in the database and
                                                                                                    //sets the default value that is returned if the database returns DBNull.
            using (Utilities.SQL.MicroORM.MicroORM ORM = new Utilities.SQL.MicroORM.MicroORM())
            {
                //Loads All
                IEnumerable<MyClass> AllItems = ORM.Map<MyClass>().All();
                
                //Loads All using constraints (Only loads first 10 items [only loading the IDs], sorted by the Name in ascending order, using the ObjectCreator Func to create each item)
                AllItems = ORM.Map<MyClass>().All(Columns: "ID", Limit: 10, OrderBy: "Name ASC", ObjectCreator: () => new MyClass(DateTime.Now));

                //Inserts an item and populates the ID field with the newly created ID (since our primary key is autoincrement)
                foreach (MyClass Item in AllItems)
                    ORM.Map<MyClass>().Insert<int>(Item, new Parameter<string>("Value to send to the database", "NameOfDatabaseField"));

                //Updates an item in the database
                foreach (MyClass Item in AllItems)
                    ORM.Map<MyClass>().Update(Item);

                //Inserts or Updates an item based on a number of criteria (see code comments to see the logic it uses)
                ORM.Map<MyClass>().Save<int>(AllItems);

                //Deletes a list of items from the database
                ORM.Map<MyClass>().Delete(AllItems);
            }

            //Uses the connection string saved at Database2
            using (Utilities.SQL.MicroORM.MicroORM ORM = new Utilities.SQL.MicroORM.MicroORM("Database2"))
            {
                //Can also drop down to straight SQL queries that return datasets, etc.

                //Sets the command
                ORM.Command = "CommandToRun";
                //Sets the command type
                ORM.CommandType = CommandType.StoredProcedure;
                //Adds the parameter to send to the database
                ORM.AddParameter("ParameterName", 2);
                //Executes a read based query
                ORM.ExecuteReader();
                //Goes to the next record
                while (ORM.Read())
                {
                    //Gets the SomeReturnedValue that was returned.
                    string SomeReturnedValue = ORM.GetParameter("SomeReturnedValue", "This is the default value if the database returns DBNull");
                }
            }

Last edited Jul 23, 2011 at 4:11 PM by JaCraig, version 2

Comments

No comments yet.