private async Task <SQLiteConnection> CreateConnectionAsync(CancellationToken cancellationToken = default(CancellationToken)) { var con = new SQLiteConnection(ConnectionString); await con.OpenAsync(cancellationToken).ConfigureAwait(false); //TODO: Add in needed PRAGMA statements return(con); }
// ADO.NETも利用可能(Microsoft.EntityFrameworkCore.Sqliteと一緒にMicrosoft.Data.Sqliteも入っている) public static async Task AdoDotNetSampleAsync() { using (var conn = new Microsoft.Data.Sqlite.SqliteConnection(ArticleContext._connectionString)) { await conn.OpenAsync(); var cmd = conn.CreateCommand(); cmd.CommandText = "SELECT * FROM Articles"; using (var reader = cmd.ExecuteReader()) while (await reader.ReadAsync()) { Console.WriteLine($"{reader.GetInt32(0)} - {reader.GetString(1)}, {reader.GetString(2)}"); } } }
public async ValueTask ConnectAsync(CancellationToken cancel) { if (_db == null) { var connectionString = _config.ConnectionString; _logger.LogTrace("Opening connection to SQLite database: " + "{ConnectionString}", connectionString); // First try to open an existing database if (!_config.MemoryOnly && System.IO.File.Exists(_config.CachePath)) { _logger.LogTrace("Found existing database at {CachePath}", _config.CachePath); var db = new SqliteConnection(_config.ConnectionString); await db.OpenAsync(); if (await CheckExistingDbAsync(db, cancel)) { // Everything checks out, we can use this as our cache db _db = db; } else { db?.Dispose(); db?.Close(); _logger.LogInformation("Deleting existing incompatible cache db file {CachePath}", _config.CachePath); System.IO.File.Delete(_config.CachePath); } } if (_db == null) { _db = new DbConnection(_config.ConnectionString); await _db.OpenAsync(); await InitializeAsync(cancel); } Commands = new DbCommandPool(_db, _logger); } }
public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default(CancellationToken)) { using (var connection = new Microsoft.Data.Sqlite.SqliteConnection(ConnectionString)) { try { await connection.OpenAsync(cancellationToken); if (TestQuery != null) { var command = connection.CreateCommand(); command.CommandText = TestQuery; await command.ExecuteNonQueryAsync(cancellationToken); } } catch (DbException ex) { return(new HealthCheckResult(status: context.Registration.FailureStatus, exception: ex)); } } return(HealthCheckResult.Healthy()); }
public Task OpenAsync() { return(connection.OpenAsync()); }
private async Task<SQLiteConnection> CreateConnectionAsync(CancellationToken cancellationToken = default(CancellationToken)) { var con = new SQLiteConnection(ConnectionString); await con.OpenAsync(cancellationToken).ConfigureAwait(false); //TODO: Add in needed PRAGMA statements return con; }