在用Xamarin Forms编写的应用程序中,我有多个具有相同表的数据库。当我登录并使用第一个数据库,然后关闭应用程序并连接到第二个数据库时,没问题,但是当我尝试连接到第一个数据库时,关闭并处理sqliteconnection并尝试在不关闭应用程序的情况下连接到第二个数据库,则表尽管连接字符串正确,但该连接将返回我的第一个数据库的记录。有什么想法吗?
这是我的代码示例:
public class Test
{
private Company company1;
private Company company2;
SQLiteAsyncConnection database1;
SQLiteAsyncConnection database2;
public Test(Company company1 , Company company2)
{
this.company1 = company1;
this.company2 = company2;
string path1 = App.SqliteManage.CompanyDatabaseFolder(company1.Name);
CompanyDatabase1(path1);
string path2 = App.SqliteManage.CompanyDatabaseFolder(company2.Name);
CompanyDatabase2(path2);
}
public async void TestMethod()
{
var db = database1;
var tables1 = await db.Table<Table>().ToListAsync();
db.GetConnection().Close();
db.GetConnection().Dispose();
var db2 = database2;
var tables2 = await db2.Table<Table>().ToListAsync();
db2.GetConnection().Close();
db2.GetConnection().Dispose();
}
public void CompanyDatabase1(string dbPath)
{
database1 = new SQLiteAsyncConnection(dbPath);
database1.CreateTableAsync<Table>().Wait();
}
public void CompanyDatabase2(string dbPath)
{
database2 = new SQLiteAsyncConnection(dbPath);
database2.CreateTableAsync<Table>().Wait();
}
}