.NET / SQLite。插入父/子记录;我如何获得外键?

时间:2010-01-30 23:01:35

标签: c# .net sqlite foreign-keys parent-child

我通过ADO.NET 2.0 Provider(SourceForce项目)使用VS 2008(C#)和SQLite。

应用程序使用的数据库包含“employees”(父级)和“employmentHistory”(子级)数据表。 “eploymentHistory”数据表应该包含从第1天开始的任何给定员工的所有工作合同(例如,促销将生成新合同)。

不幸的是,SQLite不支持外键,所以我需要自己处理数据的一致性。

jobsHistory数据库包含“id INTEGER PRIMARY KEY NOT NULL,employeeID INTEGER ...”列 很明显,employeeID将引用employeesess.ID,这是employees表中的主键。它确定了WHOSE合同。

我正在编写一种添加新员工的方法。每个人都至少有一份合同(第一份合同),因此添加新员工与添加合同有关。

以下是方法:

internal static void AddNewEmployee(Employee e, ContractChange c)
    {
        SQLiteCommandBuilder builder = new SQLiteCommandBuilder(adapter);
        var insert = new SQLiteCommand(connection);
        insert.CommandText = @"INSERT INTO employees VALUES ( null, @firstName, @lastName, @phone, @mobile, null, null, ";
        insert.CommandText+= @"@birthDate, @sex, @address, @nin, null, null); ";
        insert.Parameters.AddWithValue("firstName", e.info.name);
        insert.Parameters.AddWithValue("lastName", e.info.surname);
        insert.Parameters.AddWithValue("phone", e.info.phone);
        insert.Parameters.AddWithValue("mobile", e.info.mobile);

        insert.Parameters.AddWithValue("birthDate", e.info.DOB);
        insert.Parameters.AddWithValue("sex", e.info.sex);
        insert.Parameters.AddWithValue("address", e.info.address);
        insert.Parameters.AddWithValue("nin", e.info.NIN);

        insert.CommandText += @"INSERT INTO employmentHistory VALUES ( null, null, @startDate, 'true', @position, @contractHrs, ";
        insert.CommandText += @"@holidayAllowance, @comments, null); ";
        insert.Parameters.AddWithValue("startDate", c.date);
        insert.Parameters.AddWithValue("position", (int)c.role);
        insert.Parameters.AddWithValue("contractHrs", (int)c.contractHrs);
        insert.Parameters.AddWithValue("holidayAllowance", c.holidayAllowance);
        insert.Parameters.AddWithValue("comments", c.comments);                        

        DataTable employees = dataset.Tables[0];
        var datarowEmp = employees.NewRow();
        datarowEmp = e.ToDataRow(datarowEmp);
        employees.Rows.Add(datarowEmp);

        DataTable employmentHistory = dataset.Tables[1];
        var datarowContract = employmentHistory.NewRow();
        datarowContract = c.ToDataRow(datarowContract);
        employmentHistory.Rows.Add(datarowContract);

        adapter.UpdateCommand = insert;
        adapter.InsertCommand = insert;
        adapter.SelectCommand = new SQLiteCommand("SELECT * FROM employees; SELECT * FROM employmentHistory;", connection);
        adapter.Update(dataset);


    }

所以我想我会通过快速更新“手动”设置employeeID。但是 - 我怎么知道我刚刚添加的员​​工分配了什么ID?

令人惊讶(对我而言),即使在SQLiteDataAdapter更新数据后(我在方法的最后设置断点),此信息仍然不在数据集中。该字段仍为空。然而,SQLite确实在某个时候归因于唯一的数字,因为没有抛出任何异常并且员工确实获得了主键(我在SQLite数据库浏览器中看到了数据库)。

所以我不能自动完成,我不能手动完成 - 我应该如何强制执行数据库一致性? :(

2 个答案:

答案 0 :(得分:1)

select last_insert_rowid(); 

答案 1 :(得分:1)

请参阅last_insert_rowid

您可以在一行中实现它:

cmd.CommandText = "INSERT INTO .. VALUES(..);SELECT last_insert_rowid() AS [ID]";
strId = cmd.ExecuteScalar().ToString()

<强>更新

您可能还希望将两个插入包含在单个事务中,因此如果其中一个插入失败,您可以回滚整个插入,并防止数据库中出现损坏的数据。