1

我有一个程序,我正在将数据写入 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.

4

2 回答 2

0

它对我有用,这是小提琴

SQL小提琴

你有任何机会触发吗?

于 2013-10-26T14:05:50.783 回答
-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';
于 2013-10-26T13:58:26.293 回答