Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.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# 尝试使用C和OleDb创建外键并将其值插入.mdb数据库_C#_Ms Access_Oledb - Fatal编程技术网

C# 尝试使用C和OleDb创建外键并将其值插入.mdb数据库

C# 尝试使用C和OleDb创建外键并将其值插入.mdb数据库,c#,ms-access,oledb,C#,Ms Access,Oledb,我有一个包含3个表的数据库。其中一人与另外两人有亲戚关系。例如: Table1 = idTable1 (PK_Table1), attribute1, attribute2, attribute3 Table2 = idTable2 (PK_Table2), attribute1 Table3 = idTable3 (PK_Table3), attribute1 (FK relating to idTable1), attribute2 (FK relating to idTable2)

我有一个包含3个表的数据库。其中一人与另外两人有亲戚关系。例如:

Table1 = idTable1 (PK_Table1), attribute1, attribute2, attribute3

Table2 = idTable2 (PK_Table2), attribute1

Table3 = idTable3 (PK_Table3), attribute1 (FK relating to idTable1), attribute2 (FK relating to idTable2)
所有主键都是Access 2002版本自动分配的自动递增字段,这就是为什么my db是.mdb

在我的代码中,我使用如下代码在表1和表2中插入数据:

public void insert()
{
    string query = "INSERT INTO Table1 (attribute1,attribute2,attribute3) VALUES (?,?,?)";
    OleDbConnection dbConnection = new OleDbConnection();
    dbConnection.ConnectionString = connStr;
    try
    {
        dbConnection.Open();
        OleDbCommand commandStatement = new OleDbCommand();
        OleDbCommand primarykey = new OleDbCommand("ALTER TABLE Table1 ADD CONSTRAINT pk_Table1 primary key(idTable1)", dbConnection);
        primarykey.Connection = dbConnection;

        commandStatement.Connection = dbConnection;
        commandStatement.CommandText = query;
        commandStatement.Parameters.Add("attribute1", OleDbType.Integer).Value = attribute1;                
                commandStatement.Parameters.Add("attribute2", OleDbType.Integer).Value = attribute2;
        commandStatement.Parameters.Add("attribute3", OleDbType.Integer).Value = attribute3;

        commandStatement.ExecuteNonQuery();
        primarykey.ExecuteNonQuery(); 
        dbConnection.Close();
        dbConnection.Dispose();
        commandStatement.Dispose();
                primarykey.Dispose();
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}
表2也是这样

对于表1中的每一行,我在表2中插入大约40行,它们是包含比赛及其参赛者信息的表格

现在我需要使用表3在这两个表之间创建一个关系,表3必须引用两个表的id作为外键

这就是我迷路的地方。我不知道该怎么说,先把刚才插入到表1中的行的id,然后把刚才插入到表2中的行的id,作为新记录插入到表3中


是否有一种方法可以在插入记录后立即获取数据库分配的自动递增ID?

插入行后,可以使用SELECT@@Identity检索自动递增的值。存储它,插入另一行,执行相同的选择,然后将这些值插入链接表


有关VB.NET的详细信息,但可以在Microsoft的

上轻松理解。插入一行后,您可以使用SELECT@@Identity检索自动递增的值。存储它,插入另一行,执行相同的选择,然后将这些值插入链接表


有关VB.NET的详细信息,但可以在Microsoft的

上轻松理解。插入一行后,您可以使用SELECT@@Identity检索自动递增的值。存储它,插入另一行,执行相同的选择,然后将这些值插入链接表


有关VB.NET的详细信息,但可以在Microsoft的

上轻松理解。插入一行后,您可以使用SELECT@@Identity检索自动递增的值。存储它,插入另一行,执行相同的选择,然后将这些值插入链接表


有关VB.NET的更多信息,请访问

以从Microsoft轻松理解,非常感谢!我将解释如何在代码中使用此答案,以备将来参考,以防任何人从中受益:我创建了一个新的OleDbCommand对象来检索自动递增的值:OleDbCommand identity=new-OleDbCommand;identity.Connection=dbConnection;然后我添加了查询:identity.CommandText=SELECT@@identity;然后,由于我只希望返回1列,所以没有使用ExecuteOnQuery,而是使用ExecuteScalar:string id=Convert.ToStringidentity.ExecuteScalar;还有塔达!id变量现在保存我刚才插入的记录的id。非常感谢!我将解释如何在代码中使用此答案,以备将来参考,以防任何人从中受益:我创建了一个新的OleDbCommand对象来检索自动递增的值:OleDbCommand identity=new-OleDbCommand;identity.Connection=dbConnection;然后我添加了查询:identity.CommandText=SELECT@@identity;然后,由于我只希望返回1列,所以没有使用ExecuteOnQuery,而是使用ExecuteScalar:string id=Convert.ToStringidentity.ExecuteScalar;还有塔达!id变量现在保存我刚才插入的记录的id。非常感谢!我将解释如何在代码中使用此答案,以备将来参考,以防任何人从中受益:我创建了一个新的OleDbCommand对象来检索自动递增的值:OleDbCommand identity=new-OleDbCommand;identity.Connection=dbConnection;然后我添加了查询:identity.CommandText=SELECT@@identity;然后,由于我只希望返回1列,所以没有使用ExecuteOnQuery,而是使用ExecuteScalar:string id=Convert.ToStringidentity.ExecuteScalar;还有塔达!id变量现在保存我刚才插入的记录的id。非常感谢!我将解释如何在代码中使用此答案,以备将来参考,以防任何人从中受益:我创建了一个新的OleDbCommand对象来检索自动递增的值:OleDbCommand identity=new-OleDbCommand;identity.Connection=dbConnection;然后我添加了查询:identity.CommandText=SELECT@@identity;然后,由于我只希望返回1列,所以没有使用ExecuteOnQuery,而是使用ExecuteScalar:string id=Convert.ToStringidentity.ExecuteScalar;还有塔达!id变量现在保存我刚才插入的记录的id。