MyTetra Share
Делитесь знаниями!
Запросы с примерами
Время создания: 04.09.2019 18:55
Текстовые метки: SQLite, c#, бд, базы данных
Раздел: Компьютер - C# - SQLite
Запись: Kozlov-AE/Tetra/master/base/1565673621z5hl4cpjvp/text.html на raw.githubusercontent.com

1 Создание БД


1
2
3
4
5
6
7
8
9
10
11
12
13
14
using System;
using System.Data.SQLite;
using System.IO;
 
class Program
{
    static void Main(string[] args)
    {
        string databaseName = @"C:\cyber.db";
        SQLiteConnection.CreateFile(databaseName);
        Console.WriteLine(File.Exists(databaseName) ? "База данных создана" : "Возникла ошиюка при создании базы данных");
        Console.ReadKey(true);
    }
}

2 Создание таблицы


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
using System.Data.SQLite;
 
class Program
{
    static void Main(string[] args)
    {
        const string databaseName = @"C:\cyber.db";
        SQLiteConnection connection = 
            new SQLiteConnection(string.Format("Data Source={0};", databaseName));
        SQLiteCommand command = 
            new SQLiteCommand("CREATE TABLE example (id INTEGER PRIMARY KEY, value TEXT);", connection);
        connection.Open();
        command.ExecuteNonQuery();
        connection.Close();
        Console.ReadKey(true);
    }
}

3 Получение списка таблиц БД


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Data.Common;
using System.Data.SQLite;
 
class Program
{
    static void Main(string[] args)
    {
        const string databaseName = @"C:\cyber.db";
        SQLiteConnection connection = 
            new SQLiteConnection(string.Format("Data Source={0};", databaseName));
        connection.Open();
        SQLiteCommand command = new SQLiteCommand("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;", connection);
        SQLiteDataReader reader = command.ExecuteReader();
        foreach (DbDataRecord record in reader)
            Console.WriteLine("Таблица: " + record["name"]);
        connection.Close();
        Console.WriteLine("Готово");
        Console.ReadKey(true);
    }
}

4 Вставка записей


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System;
using System.Data.Common;
using System.Data.SQLite;
 
class Program
{
    static void Main(string[] args)
    {
        const string databaseName = @"C:\cyber.db";
        SQLiteConnection connection = 
            new SQLiteConnection(string.Format("Data Source={0};", databaseName));
        connection.Open();
        SQLiteCommand command = new SQLiteCommand("INSERT INTO 'example' ('id', 'value') VALUES (1, 'Вася');", connection);
        command.ExecuteNonQuery();
        connection.Close();
        Console.WriteLine("Готово");
        Console.ReadKey(true);
    }
}

5 Выборка строк


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
using System;
using System.Data.Common;
using System.Data.SQLite;
 
class Program
{
    static void Main(string[] args)
    {
        const string databaseName = @"C:\cyber.db";
        SQLiteConnection connection = 
            new SQLiteConnection(string.Format("Data Source={0};", databaseName));
        connection.Open();
        SQLiteCommand command = new SQLiteCommand("SELECT * FROM 'example';", connection);
        SQLiteDataReader reader = command.ExecuteReader();
        Console.Write("\u250C" + new string('\u2500', 5) + "\u252C" + new string('\u2500', 60) + "\u2510");
        Console.WriteLine("\n\u2502" + "  id \u2502" + new string(' ', 30) + "value"  + new string(' ', 25)+ "\u2502");
        Console.Write("\u251C" + new string('\u2500', 5) + "\u253C" + new string('\u2500', 60) + "\u2524\n");
        foreach (DbDataRecord record in reader)
        {
            string id = record["id"].ToString();
            id = id.PadLeft(5 - id.Length, ' ');
            string value = record["value"].ToString();
            string result = "\u2502" + id + " \u2502";
            value = value.PadLeft(60 , ' ');
            result += value + "\u2502";
            Console.WriteLine(result);
        }
        Console.Write("\u2514" + new string('\u2500', 5) + "\u2534" + new string('\u2500', 60) + "\u2518");
        connection.Close();
        Console.ReadKey(true);
    }
}
 
MyTetra Share v.0.59
Яндекс индекс цитирования