Как работать с SQLite в C# читайте далее в этой статье.
Создание файла базы данных
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); } } |
Создание таблицы
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); } } |
Получение списка таблиц базы данных
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); } } |
Вставка записей
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); } } |
Выборка строк
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); } } |
Взято отсюда: http://www.cyberforum.ru/ado-net/thread362189.html
Спасибо тебе дружище. Твой блог стал настоящим спасением для меня!
Огромное спасибо.
Нигде нет нормального примера, а тут все расписано.
Еще раз благодарю.
где взять:
using System.Data.Common;
using System.Data.SQLite;
?????
using System.Data.Common не требует подключения dll файлов, посмотрите ее в вашем Visual Studio.
Для подключения using System.Data.SQLite скачайте отсюда http://www.sqlite.org/download.html и подключите файл System.Data.SQLite.dll
Рад, что зашёл на этот сайт.