OrmLite SQL Server Example

December 24th, 2014 | Posted by Robert Curlette in OrmLite | ServiceStack

ORMLite is a great example of a framework that does one thing and does it well. If we’re looking for a efficient and solid way to get and put data in a DB within .Net (or Mono) then the ServiceStack ORMLite micro-orm is a great choice. It was created by Demis Bellot. One of the great things about ServiceStack is the high level of support from Demis and you can find many of his answers on StackExchange. In this post I will explain how to quickly setup a connection to SQL Server and add / query 1 row. If you have used ORMLite before this article might not shed any new light into the ORMLite scenario for you. Note that the current version of OrmLite (v4) is paid software and uses a quota system for small projects and evaluations. The license allows a developer to use it for a DB of up to 10 tables for free.

Getting Started

1. Create a new .Net Console Application
2. Add a NuGet reference to OrmLite.SqlServer library

Prepare SQL Server

1. Create a new Database User (if needed) for your new DB.
2. Create a new Database (ORMLite does not do this for you)
3. Give the DB User permissions to the new DB
4. Create the connection string: “Server=Dev2011;Database=ItemInfoDb;User Id=MyNewDbUser;Password=S3cr3tPw;”

Write the code

The below code creates an instance of a POCO, and then saves it to the DB. Note the AutoIncrement data attribute. This creates our PK.

For more in-depth information I highly recommend the official ServiceStack documentation. and ORMLite example here.

You can follow any responses to this entry through the RSS 2.0 You can leave a response, or trackback.

Leave a Reply

Your email address will not be published. Required fields are marked *