forked from dotnet/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
58 lines (50 loc) · 1.75 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Diagnostics;
using Microsoft.Data.Sqlite;
namespace BulkInsertSample
{
class Program
{
static void Main()
{
var connection = new SqliteConnection("Data Source=:memory:");
connection.Open();
var createCommand = connection.CreateCommand();
createCommand.CommandText =
@"
CREATE TABLE data (
value INTEGER
)
";
createCommand.ExecuteNonQuery();
Console.WriteLine("Inserting 150,000 rows...");
var stopwatch = Stopwatch.StartNew();
// There is no special API for inserting data in bulk. For the best performance,
// follow this pattern of using a transaction and re-using the same parameterized
// command.
#region snippet_BulkInsert
using (var transaction = connection.BeginTransaction())
{
var command = connection.CreateCommand();
command.CommandText =
@"
INSERT INTO data
VALUES ($value)
";
var parameter = command.CreateParameter();
parameter.ParameterName = "$value";
command.Parameters.Add(parameter);
// Insert a lot of data
var random = new Random();
for (var i = 0; i < 150_000; i++)
{
parameter.Value = random.Next();
command.ExecuteNonQuery();
}
transaction.Commit();
}
#endregion
Console.WriteLine($"Done. (took {stopwatch.ElapsedMilliseconds} ms)");
}
}
}