CRUD

Alejandro Cernuda



Create, read, update and delete (CRUD) These are the basic operations performed on a dataset. They are also the most common. All methods of the library exist in their synchronous and asynchronous versions. Whenever possible, we recommend using the latter.

Simple CRUD Examples

publicasync TaskTestInsertAsyn(User user)

{

    DataGather dg = DataGather.GetInstance(ConnectionString);

await dg.InsertAsync(user);

}

As you can see, if there is a table named User in the database and it has the same fields as the User object and also a primary key, you do not need to specify anything else.

public User: IAR

{

...

}

And much better if the User class inherits from IAR, an abstract class created in the library to provide some of the most common fields in the table and that provide comfort when working.

 

///

/// When inheriting from it, any class in the database will have fields normally used in a sql Table; as well as its initialization.

///

publicabstractclassIAR

{

publicint Id { get; set; }

///

/// This property will be initialized as true.

///

publicbool Active { get; set; }

///

/// This property will be initialized with the current date

///

public DateTime RowUpdateDate { get; set; }

publicIAR()

    {

        Active = true;

        RowUpdateDate = DateTime.Now;

}

}

Get Example

publicasync Task<User> GetAsync()

{

return (await dg.GetAsync<User>()).ToList();

}

We can also delete records in a simple way, as long as the table has a primary key.

await dg.DeleteAsync(user)

await dg.UpdateAsync(user);

Obtaining data from multiple tables

In real life it is difficult to structure the data so that there is always a table or view for the class we need in the code. SQLClientCoreTool can work with this incongruity. The following example requires bringing all the data from the PostGroup table and also the language from the Langs table. Note that instead of a query en string we could pass the name of a stored procedure and have the SQl code on the server itself.

  public class PostGroupView

    {

        public int Id { get;set; }

        public string Title { get;set; }

        public string Link { get;set; }

        public string Lang { get;set; }

    }

  public static List<PostGroupView?> GetAllBySiteId(int siteId)

        {

            string query = GetAllBySiteIdQuery(siteId);

            DataGatherdg = DataGather.GetInstance(SiteConst.ConnectionString);

            return dg.Get<PostGroupView>(query, false).ToList();

        }

 

  private static string GetAllBySiteIdQuery(int siteId)

        {

            StringBuilder sb = new StringBuilder();

            sb.AppendLine($"select distinct pg.*, l.Lang from PostsGroup pg  ");

            sb.AppendLine($"join PostsMeta pm on pg.Id = pm.PostsGroupId and pg.Active = 1 ");

            sb.AppendLine($"join Posts p on pm.PostId = p.Id and p.Active = 1 ");

            sb.AppendLine($"join Langs l on pm.LanguageId= l.Id ");

            sb.AppendLine($"where p.SiteId = {siteId} ");

            return sb.ToString();

        }