如何从异步PCL版本的SQLite中使用SQLiteAsyncConnection?

Kru*_*lur 14 c# sqlite xamarin.ios xamarin.android windows-phone-8

我正在使用来自https://github.com/oysteinkrog/SQLite.Net-PCL的PCL版本的Sqlite.net

但是,我无法进行数据库连接设置.该SQliteAsyncConnection确实不像原来的版本不是一个字符串(路径DB),但[Func< SQLiteConnectionWithLock>.]2

怎么用这个?一般来说:如何使用这个库?我有一个核心PCL lib,可以完成iOS,Android和WP8项目的所有业务逻辑.我的理解是我可以将Sqlite-Net Async PCL放入我的PCL lib中.但似乎我必须提供一些特定于平台的东西才能让它发挥作用.

Joh*_*bot 28

您只需要创建一个返回a SQLiteConnectionWithLock并将其传递给SQLiteAsyncConnection构造函数的函数.

string databasePath = "path";
var connectionFactory = new Func<SQLiteConnectionWithLock>(()=>new SQLiteConnectionWithLock(new SQLitePlatformWinRT(), new SQLiteConnectionString(databasePath, storeDateTimeAsTicks: false)));
var asyncConnection = new SQLiteAsyncConnection(connectionFactory);
Run Code Online (Sandbox Code Playgroud)

  • 如果您的连接工厂不断发布新连接,您最终会遇到SQLite忙碌异常...您可能更好地创建单个连接,并让连接工厂返回该连接. (7认同)