使用SQlite处理VisualStudio
我用
创建表格Dim mainTable = "CREATE TABLE IF NOT EXISTS users (userID INTEGER PRIMARY KEY, type VARCHAR(20))"
Dim tableA = "CREATE TABLE IF NOT EXISTS tableA (partA VARCHAR(20), userID INTEGER, FOREIGN KEY(user) REFERENCES users(userID) ON DELETE CASCADE)"
在我要在级联上删除的子项中,我知道 userIDtoDelete 的值(以前在DataGrid上选择)并且我有这个连接
Dim connDeleteUser =新SQLiteConnection(“Data Source = MyDataBase.sqlite; Version = 3”)
Try
Using (connDeleteUser)
connDeleteUser.Open()
Dim sql = "DELETE FROM actos WHERE userID = ?"
Dim connDeleteUser As SQLiteCommand = New SQLiteCommand(sql, connDeleteUser)
connDeleteUser.CommandText = sql
connDeleteUser.Parameters.AddWithValue("@userID", userIDtoDelete)
connDeleteUser.ExecuteNonQuery()
'I don´t know if this line is neccesary
connDeleteUser.Close()
End Using
Catch ex As Exception
MsgBox(ex.ToString())
End Try
我知道我需要声明PRAGMA foreign_keys = ON但我不知道如何
我尝试过这样的事情:
PRAGMA foreign_keys = ON
connDeleteUser.PrepareRetries(@"PRAGMA foreign_keys = ON)
connDeleteUser.CommandText = "PRAGMA foreign_keys = ON"
Dim sql = "DELETE FROM users WHERE userID = ? PRAGMA foreign_keys = ON"
答案 0 :(得分:2)
PRAGMA是一个SQL语句;它必须像任何其他SQL语句一样执行:
someCommand.CommandText = "PRAGMA foreign_keys = ON"
someCommand.ExecuteNonQuery
无论如何,您只需将Foreign Keys = True
选项添加到连接字符串即可。