Knight Data Access Layer
Mapping SQL
Basic Tasks C# > Mapping > Mapping SQL

One of the exciting things about Knight Data Access Layer is the ability to automatically map the results of a SQL query.  This can be performed by simply creating a class to populate the results.  Just like the other automatic mapping, the mappings are internally cached for high performance.

 

IDataHelper db = DataHelper.SessionFactory();

//Create a new Person to save to the database

Person currentPerson = new Person();

currentPerson.FirstName = "John";

currentPerson.LastName = "Smith";

//Save it to the database

db.Save(currentPerson);

string sql = "select * from Person where FirstName=@FirstName and LastName=@LastName";

//Creating dictionary

Dictionary<string, object> parms = new Dictionary<string, object>();

parms.Add("FirstName", "John");

parms.Add("LastName", "Smith");

//Load the passed sql into a list of objects

List<Person> personList = db.Load<Person>(sql, parms);