Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Desktop Developer

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Seeing the sql Simple.Data generates200 VB.Net video tutorials and many other free tutorials »
    comments

    Introduction

    After using NHibernate and Massive there was room for yet another ORM in my life. The ORM is called Simple.Data. Simple.Data was written by Mark Rendle.

    Setting it up

    I installed these packages.

    • Simple.Data.SqlCompact40 0.16.1.0
    • SqlServerCompact 4.0.8482.1

    Because those were the packages that were available on Nuget.

    So I'm gonna test Simple.Data with the SQLserver compact edition.

    Simple.Data can be used with many other databases as well. And even some nosql databases.

    To my surprise Simple.data also as a very good Manual available to you. But it only has C# samples. I'll be using VB.Net for this post.

    First I m gonna create a database.

    1. Private Function CreateDatabase() As SqlCeEngine
    2.         If File.Exists("test.sdf") Then File.Delete("test.sdf")
    3.         Dim connectionString = "DataSource=""test.sdf""; Password=""mypassword"""
    4.         Dim en = New SqlCeEngine(connectionString)
    5.         en.CreateDatabase()
    6.         Return en
    7.     End Function

    The above will delete the database when it is already there and create a new one for me.

    Then I have to add a table to my database.

    1. Private Sub CreateTablePerson()
    2.         Using cn = New SqlCeConnection("DataSource=""test.sdf""; Password=""mypassword""")
    3.             If cn.State = ConnectionState.Closed Then
    4.                 cn.Open()
    5.             End If
    6.             Dim sql = "create table Person (LastName nvarchar (40) not null, FirstName nvarchar (40))"
    7.             Using cmd = New SqlCeCommand(sql, cn)
    8.                 cmd.ExecuteNonQuery()
    9.             End Using
    10.         End Using
    11.     End Sub

    This uses the same connectionstring and adds a very simple table called Person to my database.

    Now I want to test the above. So I created a ConsoleApplication and in the Main function I add the calls to CreateDatabase and CreateTablePerson.

    That was simple so far ;-).

    The above was all SQL compact syntax and just to set us up with something to test Simple.Data with. Now on to the interesting part.

    Simple.Data

    First we need to tell Simple.Data where it can find our database.

    1. Dim db = Simple.Data.Database.OpenConnection("DataSource=""test.sdf""; Password=""mypassword""")

    this gives us a database object which we will use to query our data.

    For the next step you have to set OPTION STRICT OFF because Simple.Data uses the dynamic parts of the .Net framework.

    First thing to do is to insert some data into our table.

    1. db.Persons.Insert(New With {.LastName = "lastname1", .FirstName = "firstname1"})

    We use the db object to tell it to insert something into the table Persons. Notice how our table is called Person in the database but we can use Persons as our identifier for Simple.Data? Yeah that is awesome. But you can also write this.

    1. db.Person.Insert(New With {.LastName = "lastname1", .FirstName = "firstname1"})

    And Simple.Data will still figure it out.

    Of course a good set of integration tests is required to make sure that Simple.Data does the right thing all the time would be wise choise because we are relying on a bit of magic here to do our work for us, and magic does not always do what we expect it to do.

    Now that we have our data in the table we can query it and write it out to the console.

    1. Dim result = db.Persons.FindByLastName("lastname1")
    2.         Console.WriteLine(result.LastName & " " & result.FirstName)

    So again we use the Person table and now we use the FindBy qualifier followed by the name of our column and then we just pass our value to that, very simple.

    Go read this part of the documentation to find out how it works.

    In essence I'm also countin on it returning just one object. But what happens if multiple objects come back.

    First I make sure to insert 2 rows with the same lastname.

    1. db.Person.Insert(New With {.LastName = "lastname1", .FirstName = "firstname1"})
    2.         db.Person.Insert(New With {.LastName = "lastname1", .FirstName = "firstname2"})

    And then I do this again.

    1. Dim result = db.Persons.FindByLastName("lastname1")
    2.         Console.WriteLine(result.LastName & " " & result.FirstName)

    and that will just return the first one.

    If I want to get all persons with that lastname I have to use FindAllByLastName like so.

    1. Dim result = db.Persons.FindAllByLastName("lastname1")

    Of course now I will have to change my Console.WriteLine too.

    1. Dim result = db.Persons.FindAllByLastName("lastname1")
    2.         For Each person In result
    3.             Console.WriteLine(person.LastName & " " & person.FirstName)
    4.         Next

    Conclusion

    Simple.Data is pretty cool and it makes querying your database very simple and neat. But since this is all done by using the Dynamic Language Runtime I would hihgly recommend having Automated tests.

    About the Author

    User bio imageChris is awesome.
    Social SitingsTwitterHomePageLTD RSS Feed
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)