我有一个程序,我将数据写入datagridviews,当我关闭表单数据时,保存到SQL Server数据库。但是当我有子行时删除父行时遇到问题。我收到并发冲突异常。当我检查数据库时,我看到仅删除子行。
以下是我创建SQL Server表的查询:
sql = "CREATE TABLE Auto("
+ "VIN varchar(10) NOT NULL PRIMARY KEY,"
+ "Make nvarchar(50),"
+ "Model nvarchar(50),"
+ "Year smallint);";
和
sql = "CREATE TABLE Repair("
+ "ID int NOT NULL PRIMARY KEY,"
+ "VIN varchar(10) NOT NULL,"
+ "Description nvarchar(255),"
+ "Cost float)"
+ "ALTER TABLE Repair ADD FOREIGN KEY (VIN) REFERENCES Auto(VIN) ON DELETE CASCADE;";
我正在通过SqlDataAdapter
更新数据库。
答案 0 :(得分:0)
答案 1 :(得分:-1)
我不明白为什么你使用ALTER TABLE尝试这样的东西它对我来说很好:
CREATE TABLE Auto(
VIN varchar(10) NOT NULL PRIMARY KEY,
Make nvarchar(50),
Model nvarchar(50),
Year smallint
);
CREATE TABLE Repair(
ID int NOT NULL PRIMARY KEY,
VIN varchar(10) NOT NULL,
Description nvarchar(255),
Cost float,
FOREIGN KEY (VIN) REFERENCES Auto(VIN) ON DELETE CASCADE)
);
删除示例:
DELETE FROM Auto WHERE VIN = 'BMW';