0% found this document useful (0 votes)
238 views1 page

Adding 1 Million Records To SQLite Database in C# (Bulk Insert Records)

This C# program connects to a SQLite database, creates a table to store student records if it doesn't already exist, inserts 1 million records into the table in a loop using transactions, and outputs the elapsed time for the insert operation. It opens a connection to the database, defines and executes the CREATE TABLE SQL statement, inserts 1 million records with the name "Ali" and surname "Bak" in a loop within a transaction block, outputs a message on completion, and closes the connection.

Uploaded by

Diksin Baba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
238 views1 page

Adding 1 Million Records To SQLite Database in C# (Bulk Insert Records)

This C# program connects to a SQLite database, creates a table to store student records if it doesn't already exist, inserts 1 million records into the table in a loop using transactions, and outputs the elapsed time for the insert operation. It opens a connection to the database, defines and executes the CREATE TABLE SQL statement, inserts 1 million records with the name "Ali" and surname "Bak" in a loop within a transaction block, outputs a message on completion, and closes the connection.

Uploaded by

Diksin Baba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

class Program

static void Main(string[] args)


{
SQLiteConnection con;
SQLiteCommand cmd;

con = new SQLiteConnection("Data Source=OrnekVt.sqlite;Version=3;");


string sql = @"CREATE TABLE IF NOT EXISTS Ogrenci(
OgrNo INTEGER PRIMARY KEY AUTOINCREMENT ,
Ad TEXT NOT NULL,
Soyad TEXT NOT NULL
);";

con.Open();
cmd = new SQLiteCommand(sql, con);
cmd.ExecuteNonQuery();

/* 1 Milyon kayıt ekleme*/

var stopwatch = new Stopwatch();


stopwatch.Start();
cmd = new SQLiteCommand();

using (cmd = new SQLiteCommand(con))


{
using (var transaction = con.BeginTransaction())
{
for (var i = 0; i < 1000000; i++)
{
cmd.CommandText = "insert into Ogrenci(Ad,Soyad) values
('Ali','Bak')";
cmd.ExecuteNonQuery();
}

transaction.Commit();
}
}

Console.WriteLine("****** 1 Milyon Kayıt Eklendi *****");


Console.WriteLine("{0} saniye sürdü", stopwatch.Elapsed.TotalSeconds);

con.Close();

Console.ReadLine();

}
}

You might also like