2
允许使用SQLite的LINQPad的IQ Connection插件有一个“创建数据库如果丢失”的复选框,但只会创建一个空文件。无论如何,当文件不存在时自动构建表格?LINQPad在C#查询中创建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();
}
我希望我能拥有这一切组合成一个.linq文件正确的查询语言查询该不会需要修改任何时候sqlite数据库不存在。但我知道我在那里问很多:) – Thymine