2011-11-17 89 views
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(); 
} 

回答

2

只需设置查询语言下拉菜单“SQL”,键入DDL,然后按F5。例如:

PRAGMA foreign_keys = ON 
GO 

create table Customer 
(
    ID int not null primary key, 
    Name nvarchar(30) not null 
) 
GO 

create table Purchase 
(
    ID int not null primary key, 
    CustomerID int null references Customer (ID), 
    Date datetime not null, 
    Description varchar(30) not null, 
    Price decimal not null 
) 

(注创建外键约束的语法。)

一旦您完成后,用鼠标右键单击架构资源管理的连接,并选择“刷新”。然后,您可以切换查询语言回C#表达C#声明,并开始:)

+0

我希望我能拥有这一切组合成一个.linq文件正确的查询语言查询该不会需要修改任何时候sqlite数据库不存在。但我知道我在那里问很多:) – Thymine