1
在使用c#代码和Visual Studio或SQLite创建我的数据库时使用SQLite数据库专家个人一切工作正常。当试图添加数据到数据库使用SQLite专家个人我的外键约束工作正常,但是当我尝试插入数据使用C#我可以添加数据违反外键约束。有人可以告诉我为什么吗?SQLite中的外键
继承人我的C#代码:
SQLiteCommand cmd = new SQLiteCommand("INSERT INTO tblLicenseInfo
(UserID, MachineID, ExpirationDate, DateOfChange, LicenseKey)
VALUES (2, 1, '2014-04-03 12:15:00', '2014-04-03 12:15:00', '123123erer')", SqLite);
在孤单与tblUser
没有UserID = 2
进入和与MachineID = 1
没有进入。当我在Expert Personal中尝试使用相同的SQL语句时,它显示错误消息,但在Visual Studio中不会引发任何异常 - 数据只是插入到我的表中(当我用Expert Personal打开数据库时,可以看到条目)。
非常感谢。
编辑:
我现在又增加了一些代码,所以它看起来是这样的:
SQLiteCommand foo = new SQLiteCommand("PRAGMA foreign_keys = ON;", SqLite);
SQLiteCommand cmd = new SQLiteCommand("INSERT INTO tblLicenseInfo
(UserID, MachineID, ExpirationDate, DateOfChange, LicenseKey)
VALUES (2, 1, '2014-04-03 12:15:00', '2014-04-03 12:15:00', '123123erer')", SqLite);
SqLite.Open();
foo.ExecuteNonQuery();
cmd.ExecuteNonQuery();
SqLite.Close();
这仍然是错的?我仍然可以将数据插入到我的数据库中。