Appending two string builders

The most performant way of appending two string builders in .NET Framework 4, use this;

frontStringBuilder.Append(backStringBuilder);

For .NET Framework 2.0/3.5, use this;

frontStringBuilder.Append(backStringBuilder.ToString(0, backStringBuilder.Length));

This wouldn’t hurt performance in .NET FW 4.0.

Resources

https://stackoverflow.com/questions/6454392/how-to-append-two-stringbuilders

How to create midnight datetime

Midnight is the first one – It’s the very first second of the day, not the last.

var todayDateTime = DateTime.Today;
var Midnight = todayDateTime.AddSeconds(-1);
//Here are the results
Console.WriteLine($"Today {todayDateTime.ToString()}, Mid night {Midnight.ToString()}");

The output is this;

Today 10/8/2021 12:00:00 AM, Mid night 10/7/2021 11:59:59 PM

jQuery Ajax – tips

jQuery Ajax is async by nature. We use to set a flag “async:false” if we need to make Ajax call sync (non-blocking). This feature has been deprecated. Here is a compile list of Ajax using jQuery;

Alternative to “async: false” for successive AJAX calls

Handling Sequential AJAX Calls using jQuery

Multiple Simultaneous Ajax Requests (with one callback) in jQuery

jQuery callback for multiple ajax calls

jQuery.when understanding

jQuery Promises – Taking action .when() multiple ajax calls are complete

Dapper – ORM example list

I am using EF for most of my ORM and data access. Recently I have tries Dapper and started liking it because of its simplicity and small footprint. Here is a list of example;

Using Dapper to fill a dataset;

Dapper returns a IDataReader when we use the ExecuteReaderAsync method. More information on this addition can be found here and here.

Use Nuget package to add Dapper. Add this to your Main class for quick demo;

static IDbConnection dbConn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlServerConnString"].ConnectionString);

Here you go with DataSet example using Dapper;

public async Task<DataSet> GetUserInformationOnUserId(int UserId)
{
    var storedprocedure = "usp_getUserInformation";
    var param = new DynamicParameters();
    param.Add("@userId", UserId);
    var list = await SqlMapper.ExecuteReaderAsync(dbConn, storedprocedure, param, commandType: CommandType.StoredProcedure);
    var dataset = ConvertDataReaderToDataSet(list);
    return dataset;
}

Here is conversion to dataset method;

public DataSet ConvertDataReaderToDataSet(IDataReader data)
{
    DataSet ds = new DataSet();
    int i = 0;
    while (!data.IsClosed)
    {
        ds.Tables.Add("Table" + (i + 1));
        ds.EnforceConstraints = false;
        ds.Tables[i].Load(data);
        i++;
    }                    
    return ds;
}

How to map class names to class properties with dapper

Manually map column name with class properties

Does Dapper supports .NET DataSets