本文整理汇总了C#中Microsoft.Data.Sqlite.SqliteConnection.Open方法的典型用法代码示例。如果您正苦于以下问题:C# SqliteConnection.Open方法的具体用法?C# SqliteConnection.Open怎么用?C# SqliteConnection.Open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Data.Sqlite.SqliteConnection
的用法示例。
在下文中一共展示了SqliteConnection.Open方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BaseDataSQLite
static BaseDataSQLite()
{
#if DEBUG
_sqliteFile = Paths.DebugSQLitePath;
#else
_sqliteFile = Paths.ReleaseSQLitePath;
#endif
//_connectionString = String.Format("Data Source=:memory:");
//_connectionString = String.Format("Data Source={0};Version=3;Pooling=True;Max Pool Size=100;Synchronous=Normal;Journal Mode=WAL", _sqliteFile);
_connectionString = String.Format("Data Source={0}", _sqliteFile);
if (!File.Exists(_sqliteFile))
{
if (!File.Exists(Paths.SqlPath))
throw new FileNotFoundException("Cannot find SQL file: " + Paths.SqlPath);
else {
// Generate database
var conn = new SqliteConnection(_connectionString);
conn.Open();
var sql = File.ReadAllText(Paths.SqlPath);
conn.Execute(sql);
conn.Dispose();
}
}
}
示例2: AddHistoricMessage
private static void AddHistoricMessage(Message message)
{
var sql = string.Format("INSERT INTO {0} (MessageId, MessageType, Topic, Timestamp, HeaderBag, Body) VALUES (@MessageId, @MessageType, @Topic, @Timestamp, @HeaderBag, @Body)", _sqliteTestHelper.TableName_Messages);
var parameters = new[]
{
new SqliteParameter("MessageId", message.Id.ToString()),
new SqliteParameter("MessageType", message.Header.MessageType.ToString()),
new SqliteParameter("Topic", message.Header.Topic),
new SqliteParameter("Timestamp", SqliteType.Text) { Value =message.Header.TimeStamp.ToString("s")},
new SqliteParameter("HeaderBag",SqliteType.Text) { Value = JsonConvert.SerializeObject(message.Header.Bag)},
new SqliteParameter("Body", message.Body.Value),
};
using (var connection = new SqliteConnection(_sqliteTestHelper.ConnectionString))
using (var command = connection.CreateCommand())
{
connection.Open();
command.CommandText = sql;
//command.Parameters.AddRange(parameters); used to work... but can't with current Sqlite lib. Iterator issue
for (var index = 0; index < parameters.Length; index++)
{
command.Parameters.Add(parameters[index]);
}
command.ExecuteNonQuery();
}
}
示例3: Create
/// <summary>
/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public virtual DatabaseModel Create(string connectionString, TableSelectionSet tableSelectionSet)
{
Check.NotEmpty(connectionString, nameof(connectionString));
Check.NotNull(tableSelectionSet, nameof(tableSelectionSet));
ResetState();
using (_connection = new SqliteConnection(connectionString))
{
_connection.Open();
_tableSelectionSet = tableSelectionSet;
string databaseName = null;
try
{
databaseName = Path.GetFileNameWithoutExtension(_connection.DataSource);
}
catch (ArgumentException)
{
// graceful fallback
}
_databaseModel.DatabaseName = !string.IsNullOrEmpty(databaseName)
? databaseName
: _connection.DataSource;
GetTables();
GetColumns();
GetIndexes();
GetForeignKeys();
return _databaseModel;
}
}
示例4: LocateIPv6Async
private async Task<IEnumerable<IPRangeInfo>> LocateIPv6Async(IPAddress ip)
{
var flatIP = new FlatIPAddress(ip);
using (var connection = new SqliteConnection(_connectionString))
{
connection.Open();
return await connection.ExecuteReaderAsync(dataReader =>
{
return new IPRangeInfo
{
IPRange = new IPRange
{
Start = new FlatIPAddress { Low = (long)dataReader["StartLow"], High = (long)dataReader["StartHigh"] }.ToIPAddress(),
End = new FlatIPAddress { Low = (long)dataReader["EndLow"], High = (long)dataReader["EndHigh"] }.ToIPAddress(),
},
AreaId = Convert.ToInt32(dataReader["AreaId"]),
ProviderId = Convert.ToInt32(dataReader["ProviderId"]),
};
}, @"
SELECT * FROM IPRangeInfos
WHERE (StartHigh < {1} OR (StartHigh = {1} AND StartLow <= {0}))
AND (EndHigh > {1} OR (EndHigh = {1} AND EndLow >= {0}));",
flatIP.Low, flatIP.High);
}
}
示例5: RunSqlite
public void RunSqlite()
{
using (var connection = new SqliteConnection("Data Source=:memory:"))
{
connection.Open();
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = "CREATE TABLE Test(Value);";
cmd.ExecuteNonQuery();
cmd.CommandText = "INSERT INTO Test VALUES(1);";
cmd.ExecuteNonQuery();
}
using (var cmd = connection.CreateCommand())
{
cmd.CommandText =
"SELECT [type], [name], [tbl_name], [rootpage], [sql], [rowid] FROM [main].[sqlite_master] WHERE [type] LIKE 'table'";
using (var dr = cmd.ExecuteReader())
{
while (dr.Read())
{
System.Console.WriteLine(dr[2].ToString());
}
}
}
var databaseReader = new DatabaseSchemaReader.DatabaseReader(connection);
var schema = databaseReader.ReadAll();
var tableList = databaseReader.TableList();
var tables = databaseReader.AllTables();
var views = databaseReader.AllViews();
Assert.NotEmpty(tableList);
}
}
示例6: Open
private SqliteConnection Open(string connectionString)
{
var connection = new SqliteConnection(connectionString);
connection.Open();
connection.EnableExtensions();
connection.LoadExtension(_spatialiteExtensionName);
return connection;
}
示例7: CreateTransient
private SqliteTestStore CreateTransient(bool sharedCache)
{
_connection = new SqliteConnection(CreateConnectionString(_name, sharedCache));
_connection.Open();
return this.AsTransient();
}
示例8: TestLite
public void TestLite()
{
using (SqliteConnection conn = new SqliteConnection(_connectionString))
{
conn.Open();
//var d = conn.Query("SELECT * FROM User;");
}
}
示例9: CreateTransient
private SqliteTestStore CreateTransient()
{
_connection = new SqliteConnection(CreateConnectionString(_name));
_connection.Open();
_deleteDatabase = true;
return this;
}
示例10: CreateAndOpenConnection
private void CreateAndOpenConnection(bool sharedCache = false)
{
_connection = new SqliteConnection(CreateConnectionString(_name, sharedCache));
_connection.Open();
var command = _connection.CreateCommand();
command.CommandText = "PRAGMA foreign_keys=ON;";
command.ExecuteNonQuery();
}
示例11: CanShowOverlay
protected override bool CanShowOverlay(string path, FILE_ATTRIBUTE attributes)
{
// Return true if the file is read only, meaning we'll show the overlay.
try
{
string app = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string dbPath = Path.Combine(app, @"yliyun\db\file_status.db");
using (SqliteConnection conn = new SqliteConnection("Data Source =" + dbPath))
{
string rootDir = @"D:\一粒云盘";
conn.Open();
SqliteCommand cmd1 = new SqliteCommand("SELECT syncRoot FROM yliyun_conf", conn);
SqliteDataReader reader1 = cmd1.ExecuteReader();
if (reader1.Read())
{
rootDir = reader1.GetString(0);
}
if (path.StartsWith(Path.Combine(rootDir, @"个人空间\"))
|| path.StartsWith(Path.Combine(rootDir, @"群组空间\"))
|| path.StartsWith(Path.Combine(rootDir, @"部门空间\"))
|| path.StartsWith(Path.Combine(rootDir, @"共享空间\")))
{
string sql = "SELECT lastModified FROM file_status WHERE filePath = @fp";
SqliteCommand cmd = new SqliteCommand(sql, conn);
cmd.Parameters.AddRange(new[]
{
new SqliteParameter("@fp", path)
});
SqliteDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
}
catch (Exception)
{
return false;
}
}
示例12: ConnectionString_setter_throws_when_open
public void ConnectionString_setter_throws_when_open()
{
using (var connection = new SqliteConnection("Data Source=:memory:"))
{
connection.Open();
var ex = Assert.Throws<InvalidOperationException>(() => connection.ConnectionString = "Data Source=test.db");
Assert.Equal(Strings.ConnectionStringRequiresClosedConnection, ex.Message);
}
}
示例13: Ctor_throws_when_invalid_isolation_level
public void Ctor_throws_when_invalid_isolation_level()
{
using (var connection = new SqliteConnection("Data Source=:memory:"))
{
connection.Open();
var ex = Assert.Throws<ArgumentException>(() => connection.BeginTransaction(IsolationLevel.Snapshot));
Assert.Equal(Strings.FormatInvalidIsolationLevel(IsolationLevel.Snapshot), ex.Message);
}
}
示例14: ConstructRelationalModel
public override IModel ConstructRelationalModel([NotNull] string connectionString)
{
Check.NotEmpty(connectionString, nameof(connectionString));
var modelBuilder = new ModelBuilder(new ConventionSet());
using (var connection = new SqliteConnection(connectionString))
{
connection.Open();
var tables = new Dictionary<string, string>();
var indexes = new List<SqliteIndexInfo>();
var master = connection.CreateCommand();
master.CommandText = "SELECT type, name, sql, tbl_name FROM sqlite_master";
using (var reader = master.ExecuteReader())
{
while (reader.Read())
{
var type = reader.GetString(0);
var name = reader.GetString(1);
var sql = reader.GetValue(2) as string; // can be null
var tableName = reader.GetString(3);
if (type == "table"
&& name != "sqlite_sequence"
&& _tableSelectionSet.Allows(TableSelection.Any, name))
{
tables.Add(name, sql);
}
else if (type == "index")
{
indexes.Add(new SqliteIndexInfo
{
Name = name,
TableName = tableName,
Sql = sql
});
}
}
}
LoadTablesAndColumns(connection, modelBuilder, tables.Keys);
LoadIndexes(connection, modelBuilder, indexes);
foreach (var item in tables)
{
SqliteDmlParser.ParseTableDefinition(modelBuilder, item.Key, item.Value);
}
AddAlternateKeys(modelBuilder);
LoadForeignKeys(connection, modelBuilder, tables.Keys);
}
return modelBuilder.Model;
}
示例15: CreateShared
private SqliteTestStore CreateShared(Action initializeDatabase)
{
CreateShared(typeof(SqliteTestStore).Name + _name, initializeDatabase);
_connection = new SqliteConnection(CreateConnectionString(_name));
_connection.Open();
_transaction = _connection.BeginTransaction();
return this;
}