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

    « Simple.Data with VB.Net sample solution up on GithubPetaPoco: Mapping related objects »
    comments

    Introduction

    In this last post I want to show some more complex queries. But I also want to remind you that you need to know when to quit. When queries get to complex you will have to jump through hoops to get them working with any ORM while it is all very "easy" to do in SQL. In other words use the right tool for the job.

    Setup

    Here is the code to set everything up.

    1. Private Sub InsertTestData(ByVal db As Object)
    2.         db.Address.Insert(New With {.Street = "street1", .HouseNumber = "1"})
    3.         db.Address.Insert(New With {.Street = "street2", .HouseNumber = "2"})
    4.         db.Person.Insert(New With {.LastName = "lastname1", .FirstName = "firstname1", .AddressId = 1})
    5.         db.Person.Insert(New With {.LastName = "lastname1", .FirstName = "firstname2", .AddressId = 1})
    6.         db.Person.Insert(New With {.LastName = "lastname2", .FirstName = "firstname3", .AddressId = 2})
    7.         db.BadHabit.Insert(New With {.BadHabit = "Drinks"})
    8.         db.BadHabit.Insert(New With {.BadHabit = "Smokes"})
    9.         db.BadHabit.Insert(New With {.BadHabit = "Eats to much"})
    10.         db.BadHabitPerson.Insert(New With {.BadHabitId = 1, .PersonId = 1})
    11.         db.BadHabitPerson.Insert(New With {.BadHabitId = 2, .PersonId = 1})
    12.         db.BadHabitPerson.Insert(New With {.BadHabitId = 3, .PersonId = 2})
    13.         db.BadHabitPerson.Insert(New With {.BadHabitId = 3, .PersonId = 1})
    14.         db.BadHabitPerson.Insert(New With {.BadHabitId = 3, .PersonId = 3})
    15.     End Sub
    16.  
    17.     Public Function ConnectionString() As String
    18.         Return "DataSource=""test.sdf""; Password=""mypassword"""
    19.     End Function
    20.  
    21.     Private Function CreateDatabase() As SqlCeEngine
    22.         If File.Exists("test.sdf") Then File.Delete("test.sdf")
    23.         Dim en = New SqlCeEngine(ConnectionString)
    24.         en.CreateDatabase()
    25.         Return en
    26.     End Function
    27.  
    28.     Private Sub CreateTables()
    29.         Using cn = New SqlCeConnection(ConnectionString)
    30.             If cn.State = ConnectionState.Closed Then
    31.                 cn.Open()
    32.             End If
    33.             For Each SqlScript In CreateTableScripts()
    34.                 Using cmd = New SqlCeCommand(SqlScript, cn)
    35.                     cmd.ExecuteNonQuery()
    36.                 End Using
    37.             Next
    38.         End Using
    39.     End Sub
    40.  
    41.     Private Function CreateTableScripts() As IList(Of String)
    42.         Dim s As New List(Of String)
    43.         s.Add("CREATE TABLE Address (Id int IDENTITY(1,1) PRIMARY KEY, Street nvarchar (40) NOT NULL, HouseNumber nvarchar (10))")
    44.         s.Add("CREATE TABLE Person (Id int IDENTITY(1,1) PRIMARY KEY, LastName nvarchar (40) NOT NULL, FirstName nvarchar (40), AddressId int NOT NULL)")
    45.         s.Add("ALTER TABLE Person ADD CONSTRAINT FK_Person_Address FOREIGN KEY (AddressId) REFERENCES Address(Id)")
    46.         s.Add("CREATE TABLE BadHabit (Id int IDENTITY(1,1) PRIMARY KEY, BadHabit nvarchar (40) NOT NULL)")
    47.         s.Add("CREATE TABLE BadHabitPerson (BadHabitId int NOT NULL, PersonId int NOT NULL)")
    48.         s.Add("ALTER TABLE BadHabitPerson ADD PRIMARY KEY(BadHabitId, PersonId)")
    49.         s.Add("ALTER TABLE BadHabitPerson ADD CONSTRAINT FK_BadHabitPerson_Person FOREIGN KEY (PersonId) REFERENCES Person(Id)")
    50.         s.Add("ALTER TABLE BadHabitPerson ADD CONSTRAINT FK_BadHabitPerson_BadHabit FOREIGN KEY (BadHabitId) REFERENCES BadHabit(Id)")
    51.         Return s
    52.     End Function

    queries

    To get the person with the streetname "street1" you can should do this in C#

    1. db.Persons.FindAll(db.Persons.Address.Street == "street1");

    The above however does not work in VB.Net.

    But this does.

    1. db.Person.FindAll(db.Address.Street = "street1").WithAddress
    1. select [Person].[Id],[Person].[LastName],[Person].[FirstName],[Person].[AddressId],[Address].[Id] AS [__with1__Address__Id],[Address].[Street] AS [__with1__Address__Street],[Address].[HouseNumber] AS [__with1__Address__HouseNumber] from [Person] LEFT JOIN [Address] ON ([Address].[Id] = [Person].[AddressId]) WHERE [Address].[Street] = @p1
    2. @p1 (String) = street1

    The above works but just because I use WithAddress and setup referential integrity.

    I can also use like. Like this.

    1. db.Person.FindAll(db.Address.Street.Like("street%")).WithAddress
    1. select [Person].[Id],[Person].[LastName],[Person].[FirstName],[Person].[AddressId],[Address].[Id] AS [__with1__Address__Id],[Address].[Street] AS [__with1__Address__Street],[Address].[HouseNumber] AS [__with1__Address__HouseNumber] from [Person] LEFT JOIN [Address] ON ([Address].[Id] = [Person].[AddressId]) WHERE [Address].[Street] LIKE @p1
    2. @p1 (String) = street%

    I can also select which columns I want to return.

    1. db.Person.All.Select(db.Person.LastName)
    1. select [Person].[LastName] from [Person]

    I can also give my columns aliases.

    1. db.Person.All.Select(db.Person.LastName.As("Name"))
    1. select [Person].[LastName] AS [Name] from [Person]

    And you can do much more.
    But I think I have learned enough about Simple.Data now. And it's time to move on to something else.

    Conclusion

    I really like Simple.Data and I will surely use it again in the future. But if you want to keep your sanity I would recommend using C# and not VB.Net. The whole dynamic thing is very quirky in VB.Net.

    You should however know the limits of the framework you are using and keep in mind that some things are just better solved another way. It will give you less headaches by trying to fit a square peg in a round hole and it will make for a more enjoyable experience all around. I see way to many people trying to use whatever they have to do whatever they need to do and then hit a brick wall. So stop doing that and use Simple.Data for what it was supposed to do quick and easy CRUD.

    About the Author

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

    1 comment

    Comment from: Eli Weinstock-Herman (tarwn) [Member]
    Eli Weinstock-Herman (tarwn) I meant to ask, can Simple.Data do partial results? Like if I just wanted a few fields from a given table or JOIN instead of all the fields?
    05/08/12 @ 04:47

    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.)