【发布时间】:2011-11-17 02:04:13
【问题描述】:
允许使用 SQLite 的 LINQPad 的 IQ Connection 插件有一个“如果丢失则创建数据库”复选框,但这只会创建一个空文件。文件不存在时是否自动建表?
不应该有一种方法来获取 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();
}
【问题讨论】: