Save, Update and Get data from SQL using Fluent NHibernate


Ahamed Fazil Buhari
SharePoint Developer
Published On :   30 Nov 2017
Visit Count
Today :  2    Total :   134
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


Hello everyone,

This article is continuation of my previous two articles – “Configuration of Fluent NHibernate through C# code” and “Handling Parent Child SQL Table relation using Fluent NHibernate”. Once we configured and mapped the data, we need to save it in SQL and this is our major goal.

We can achieve bulk update easily using Fluent nhibernate and it gives very good performance. Call the below function with the list of package.

 public static void SaveOrUpdatePackage(List<TablePackage> packages)
         {        
             using (var session = _sessionFactory.OpenSession())
             {
                 using (var transaction = session.BeginTransaction())
                 {
                     session.SetBatchSize(1000);
                     foreach (var package in packages)
                     {
   //If you don’t want to update and you want only to save
   //_session.Save(package);
                        session.SaveOrUpdate(package);
                     }
                     transaction.Commit();
                 }
             }
         }
 

To get all the data from Database

 public IList<string> GetTableDetails()
         {
             using (var session = _sessionFactory.OpenSession())
             {
                 var tableIDs = session.QueryOver<TablePackage>()
 //Here we are selecting only ID, you do multiple selection as //well
                              .Select(x => x.ID)
                              .OrderBy(x => x.ID)
                              .Asc.List<string>();
                 return tableIDs;
             }
         }
 

To get data based on ID –

 public string GetTableDetailById(string ID)
         {
             using (var session = _sessionFactory.OpenSession())
             {
                 var tableValue = session.Get<TablePackage>(ID);
   //Convert table value into JSON object
                 return JsonConvert.SerializeObject(tableValue);
             }
         }
 

Happy Coding

Ahamed

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

KWizCom Scan