You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.2 KiB

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SQLite_Console
{
class Program
{
static void Main(string[] args)
{
string connString = @"Data Source = D:\SQLite\mydb.db";
using (SQLiteConnection conn = new SQLiteConnection(connString))
{
Stopwatch watch = new Stopwatch();
watch.Start();
conn.Open();
watch.Stop();
Console.WriteLine($"open: {watch.ElapsedMilliseconds}ms");
Console.WriteLine();
watch.Restart();
Random rnd = new Random();
for (int i = 0; i < 2000; i++)
{
string guid = Guid.NewGuid().ToString();
int age = rnd.Next(1, 100);
string name = $"Robot#{i + 1}";
string insertQry = $"INSERT INTO member VALUES ('{name}', {age}, '{guid}')";
SQLiteCommand cmd = new SQLiteCommand(insertQry, conn);
cmd.ExecuteNonQuery();
}
watch.Stop();
Console.WriteLine($"insert: {watch.ElapsedMilliseconds}ms");
Console.WriteLine();
watch.Restart();
DataSet ds = new DataSet();
string selectQry = "SELECT * FROM member";
SQLiteDataAdapter adt = new SQLiteDataAdapter(selectQry, conn);
adt.Fill(ds);
DataRowCollection rows = ds.Tables[0].Rows;
PrintRows(rows);
watch.Stop();
Console.WriteLine($"select all: {watch.ElapsedMilliseconds}ms");
Console.WriteLine();
watch.Restart();
selectQry = "SELECT * FROM member WHERE age < 50";
adt = new SQLiteDataAdapter(selectQry, conn);
adt.Fill(ds);
rows = ds.Tables[0].Rows;
PrintRows(rows);
watch.Stop();
Console.WriteLine($"select under 50: {watch.ElapsedMilliseconds}ms");
Console.WriteLine();
watch.Restart();
}
Console.ReadKey();
}
private static void PrintRows(DataRowCollection rows)
{
Console.WriteLine($"found {rows.Count} rows.");
foreach (DataRow row in rows)
{
string name = (string)row["name"];
long age = DBNull.Value.Equals(row["age"]) ? -1 : (long)row["age"];
string guid = (string)row["guid"];
Console.WriteLine($"name: {name}, age: {age}, guid: {guid}");
}
}
}
}