本文介绍了尝试使用OleDb创建外键并将其值插入.mdb数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3个表的数据库.其中一个拥有其他两个的关系.例如:

I have a database with 3 tables. One of them holds a relation of the other two. E.g.:

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

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

Table2 = idTable2(PK_Table2),attribute1

Table2 = idTable2 (PK_Table2), attribute1

Table3 = idTable3(PK_Table3),attribute1(与idTable1相关的FK),attribute2(与idTable2相关的FK)

Table3 = idTable3 (PK_Table3), attribute1 (FK relating to idTable1), attribute2 (FK relating to idTable2)

所有主键都是Access自动分配的自动递增字段(2002版,这就是我的数据库是.mdb的原因).

All primary keys are autoincrementing fields assigned automatically by Access (2002 version, that's why my db is a .mdb).

在我的代码中,我使用如下代码在Table1和Table2中插入数据:

In my code I insert data in Table1 and Table2 using some code like this:

public void insert() { string query = "INSERT INTO Table1 (attribute1,attribute2,attribute3) VALUES (?,?,?)"; OleDbConnection dbConnection = new OleDbConnection(); dbConnection.ConnectionString = connStr;

试试{dbConnection.Open();OleDbCommand commandStatement =新的OleDbCommand();OleDbCommand primarykey = new OleDbCommand("ALTER TABLE Table1 ADD CONSTRAINT pk_Table1 primary key(idTable1)",dbConnection);primarykey.Connection = dbConnection;commandStatement.Connection = dbConnection;commandStatement.CommandText =查询;commandStatement.Parameters.Add("attribute1",OleDbType.Integer).Value = attribute1;

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();}捕获(异常e){Console.WriteLine(e.Message);}}

                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);}}

(然后也适用于Table2).

(and then something like that for Table2 as well).

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

For each row in Table1, I insert about 40 rows in Table2 (they're tables holding info of a contest and their contestants).

现在,我需要使用Table3在这两个表之间创建一个关系,该表必须将两个表的ID都作为外键引用.

Now I need to create a relation between those two, using Table3, which must reference the id of both tables as foreign keys.

那是我迷路的地方.我不知道怎么说将您刚刚插入到Table1中的行的ID,然后将您刚刚插入到Table2中的行的ID,然后将它们作为新记录插入到Table3中".

And that's where I'm lost. I don't know how to say "take the id of the row you just inserted in Table1 and then the id of a row you just inserted in Table2 and insert them as a new record in Table3".

插入记录后,有没有办法获取数据库分配的自动增量ID?

Is there a way to get the autoincrementing IDs that are being assigned by the database, as soon as I insert a record?

谢谢!

推荐答案

开始翻译
  插入第一张表
   id = SCOPE_IDENTITY()
  插入第二张表
  提交

Begin trans
   insert into first table
   id = SCOPE_IDENTITY()
   insert into second table
   commit

迈克尔·泰勒
http://blogs.msmvps.com/p3net

Michael Taylor
http://blogs.msmvps.com/p3net


这篇关于尝试使用OleDb创建外键并将其值插入.mdb数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-25 09:36