问题描述
我使用SQLite C#和有规定的外键一些表。
I'm using SQLite with C# and have some tables with foreign keys defined.
现在,我知道,在默认情况下的外键约束SQLite中不执行,但是我想将它们打开。
Now, I know that by default foreign key constraints are not enforced in SQLite, but I'd like to turn them ON.
是否有可能通过code做到这一点?我已经看过了相关的问题的,但我不知道该怎么办呢通过C#code。我使用的是最新的插件的SQLite提供的Visual Studio 2008的设计我的表。
Is it possible to do this through code? I have looked up a related question, but I'm not sure how to do it through C# code. I'm using the latest plug-in of SQLite available for Visual Studio 2008 for designing my tables.
conn.Open();
SQLiteCommand cmd = new SQLiteCommand("PRAGMA foreign_keys = ON", conn);
cmd.ExecuteNonQuery();
conn.Close();
我需要这种变化持续下去时,此连接被重新打开。这可能吗?
I need this change to persist when this connection is reopened. Is it possible?
推荐答案
终于想通了这一点,从<一个href="http://stackoverflow.com/questions/6288871/foreign-key-support-in-sqlite3?answertab=oldest#tab-top">this帖子。杂注foreign_key设置不坚持,但你可以在每次连接是在的ConnectionString 的时间设置。这使您可以使用Visual Studio的表适配器。
Finally figured this out from this post. The PRAGMA foreign_key setting does not persist but you can set it every time the connection is made in the ConnectionString. This allows you to use Visual Studio's table adapters.
- 请确保您有最新版本(1.0.73.0 system.data.sqlite安装(1.0.66.0的)将无法正常工作)。
- 更改您的的ConnectionString 到
数据源= C:\的Dbs \ myDb.db;外键= TRUE;
(替换C:\的Dbs \ myDb.db您的SQLite数据库)。
- Make sure you have the latest version (1.0.73.0) of system.data.sqlite installed (1.0.66.0 will not work).
- Change your ConnectionString to
data source=C:\Dbs\myDb.db;foreign keys=true;
(replace C:\Dbs\myDb.db with your sqlite database).
这篇关于启用在SQLite的外键约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!