LINQPad的IQ Connection插件可以使用SQLite,但“如果缺少数据库,则创建数据库”复选框只会创建一个空文件。当文件不存在时,有没有自动构建表的方法呢?
难道不能通过获取DataContext并使用该接口创建表吗?希望这样做会导致LINQPad同时更新其DataContext。
到目前为止,我能做到的最好的是下面的内容,创建DbCommands并在删除sqlite文件后的第一次运行时执行它们,然后我必须刷新数据库,并再次运行它们。
难道不能通过获取DataContext并使用该接口创建表吗?希望这样做会导致LINQPad同时更新其DataContext。
到目前为止,我能做到的最好的是下面的内容,创建DbCommands并在删除sqlite文件后的第一次运行时执行它们,然后我必须刷新数据库,并再次运行它们。
void Main()
{
if (!File.Exists(this.Connection.ConnectionString.Split('=')[1]))
{
"CREATING DATABASE TABLES".Dump();
CREATE_TABLES();
}
else
{
"RUNNING CODE".Dump();
//Code goes here...
}
}
public void CREATE_TABLES()
{
this.Connection.Open();
System.Data.Common.DbCommand sup = this.Connection.CreateCommand();
sup.CommandText = @"create table MainTable
(
MainTableID INTEGER not null PRIMARY KEY AUTOINCREMENT,
FileName nvarchar(500) not null
)";
sup.ExecuteNonQuery();
sup.CommandText = @"create table SubTable
(
SubTableID int not null,
MainTableID int not null,
Count int not null,
primary key (SubTableID, MainTableID),
FOREIGN KEY(MainTableID) REFERENCES MainTable(MainTableID)
)";
//Apparently this version of sqlite doesn't support foreign keys really
sup.ExecuteNonQuery();
this.Connection.Close();
}