Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

sqlite dapper bulkcopy

public async Task InsertInBulk(IList<string> userNames)
{
    var sqls = GetSqlsInBatches(userNames);
    using (var connection = new SqlConnection(ConnectionString))
    {
        foreach (var sql in sqls)
        {
            await connection.ExecuteAsync(sql);
        }
    }
}

private IList<string> GetSqlsInBatches(IList<string> userNames)
{
    var insertSql = "INSERT INTO [Users] (Name, LastUpdatedAt) VALUES ";
    var valuesSql = "('{0}', getdate())";
    var batchSize = 1000;

    var sqlsToExecute = new List<string>();
    var numberOfBatches = (int)Math.Ceiling((double)userNames.Count / batchSize);

    for (int i = 0; i < numberOfBatches; i++)
    {
        var userToInsert = userNames.Skip(i * batchSize).Take(batchSize);
        var valuesToInsert = userToInsert.Select(u => string.Format(valuesSql, u));
        sqlsToExecute.Add(insertSql + string.Join(',', valuesToInsert));
    }

    return sqlsToExecute;
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: Known Folders C# 
Csharp :: call Textboxfor in cs 
Csharp :: return a list of list from yaml via C# 
Csharp :: c# convert timestamp to datetime 
Csharp :: winforms reportviewer.print report 
Csharp :: how to make character respawn if touches sprite c# 
Csharp :: youtube unity 2d intercept 
Csharp :: c# encrypt folder SHA512 
Csharp :: how to collect input from a user in discord bot c# 
Csharp :: c# code for simplex method 
Csharp :: invalid length for a base-64 char array or string. frombase64string c#Add Answer 
Csharp :: How to truncate a decimal without rounding 
Csharp :: C# dictionnaries 
Csharp :: ienumerable tolist 
Csharp :: Devexpress MVC Gridview BinaryImage Picture 
Csharp :: asp.net issue 
Csharp :: add new page itext 7 
Csharp :: linq contains null 
Csharp :: create expression func c# for use in where clause 
Csharp :: textbox center align winform 
Csharp :: add file to combobox c# 
Csharp :: c# directory entry invoke 
Csharp :: uity pause game 
Csharp :: log4net.dll 
Csharp :: convert web app to linux 
Csharp :: switch case c# enum tostring 
Csharp :: dapper execute with list of ids 
Csharp :: IdentityServer vs JWT vs OAuth? 
Csharp :: visual studio auto generate and setters 
Csharp :: Align String with Spaces [C#] 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =