Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# .NET/SQLite。插入父/子记录;如何获取外键?_C#_.net_Sqlite_Foreign Keys_Parent Child - Fatal编程技术网

C# .NET/SQLite。插入父/子记录;如何获取外键?

C# .NET/SQLite。插入父/子记录;如何获取外键?,c#,.net,sqlite,foreign-keys,parent-child,C#,.net,Sqlite,Foreign Keys,Parent Child,我通过ADO.NET2.0提供程序(SourceForce项目)使用VS2008(C#)和SQLite 应用程序使用的数据库包含“employees”(父)和“employmentHistory”(子)数据表。“eploymentHistory”数据表应包含从第一天起任何给定员工的所有工作合同(例如,晋升将生成新合同) 不幸的是,SQLite不支持外键,所以我需要自己处理数据一致性 employmentHistory数据库包含“id INTEGER主键不为NULL,employeeID INTE

我通过ADO.NET2.0提供程序(SourceForce项目)使用VS2008(C#)和SQLite

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

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

employmentHistory数据库包含“id INTEGER主键不为NULL,employeeID INTEGER…”列 很明显,employeeID将引用Employeess.ID,employees表中的主键。它标识了它是谁的合同

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

方法如下:

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数据库浏览器中看到了数据库)

因此,我不能自动执行,也不能手动执行-我应该如何加强数据库一致性(

参见

您可以在一行中实现它:

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


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

FYI,性别被认为比sexWell更合适。我选中了sexWell,它的工作原理实际上很简单,如:“插入到employmentHistory值中。”(null,last_insert_rowid()…“但我知道last_insert_rowid()将返回0,如果添加员工由于任何原因出错,那么在最好的情况下,我将得到孤立的记录谢谢您的帮助!
cmd.CommandText = "INSERT INTO .. VALUES(..);SELECT last_insert_rowid() AS [ID]";
strId = cmd.ExecuteScalar().ToString()