外键约束没有SQLite中工作

问题描述 投票:0回答:1

我从表1中使用SQLite下探PK表2为外键在我的数据库。但我的表2没有相应的努力外键约束它插入记录,如果该键不在表1提供。我使用DB浏览器和我的编译外键启动存在,但其仍无法正常工作

sqlite
1个回答
1
投票

我已经找到了解决办法。您必须启用C#代码编译外键每次你建立连接。让我分享我的代码:

 conn.Open();
            string query = " insert into Installment_Details (Account_No,Month_yr,Date,Receipt_No,Amount_Received,Amount_Left,Amount_receiver) values ('" + this.Textbox_AN.Text + "','" + this.Textbox_MY.Text + "','" + this.Textbox_D.Text + "','" + this.Textbox_RN.Text + "','" + this.Textbox_AR.Text + "','" + this.Textbox_AL.Text + "','" + this.Textbox_ARR.Text + "');";
            SQLiteCommand createcommand = new SQLiteCommand("PRAGMA foreign_keys = ON", conn);
            createcommand.ExecuteNonQuery();
            createcommand = new SQLiteCommand(query, conn);
            createcommand.ExecuteNonQuery();
            MessageBox.Show("Saved Successfully");
© www.soinside.com 2019 - 2024. All rights reserved.