如何通过数据集插入具有一对一关系的表

发布于 2024-11-18 09:06:26 字数 408 浏览 2 评论 0原文

我使用 asp.net 4DataSets 来访问数据库。数据库中有两个具有一对一关系的表。这意味着两个表都具有相同的主键列(例如 Id),并且其中一个表在此列集上具有 @identity

所以一般来说,如果我们要插入,我们会先插入第一个表,然后用table1中相应记录的id.table2 = id插入第二个表。

我可以想象如何使用存储过程来实现这一点(我们将插入到第一个表中并将 id 作为输出参数,然后使用此 id 插入到第二个表中,顺便说一句,所有这些都在一个事务内)。

但是有没有一种方法可以不使用存储过程来做到这一点呢?可能 DataSets \ DataAdapters 内置有这样的功能?

将不胜感激任何帮助。

I use asp.net 4 and DataSets for accessing the database. There are two tables with one-to-one relationship in the database. It means that both tables have the same column as a primary key (say Id), and one of tables has @identity on this column set.

So in general if we want to insert, we insert first into the first table, than insert into the second table with id.table2 = id of the corresponding record in table1.

I can imagine how to achieve this using stored procedure (we would insert into the first table and have id as an out parameter and then insert into the second table using this id, btw all inside one transaction).

But is there a way to do it without using a stored procedure? May be DataSets \ DataAdapters have such functionality built in?

Would appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪都笑了 2024-11-25 09:06:26

今天这里很安静......好吧,如果有人也在寻找这样的解决方案,我已经找到了一种方法。

所以我们的主要问题是获取第一个表中新创建的记录的 id。如果我们能够做到这一点,之后我们只需将其提供给下一个方法,该方法会在第二个表中创建相应的记录。

我使用了数据集设计器来享受 VS 的代码自动生成功能。我们将第一个表称为 TripSets。在“数据集设计器”中,右键单击 TripSetsTableAdapter,然后单击“属性”。展开 InsertCommand 属性组。这里我们需要做两件事。

首先,我们使用参数集合编辑器将新参数添加到参数集合中。设置 ParameterName = @TripId、DbType = Int32(或您需要的任何内容)、Direction = Output。

其次,我们修改 CommandText(为了方便起见,使用查询生成器)。在命令末尾添加一个分号,如下所示:

(...);
SELECT @TripId = SCOPE_IDENTITY()

因此,您将得到类似以下语句的内容:

INSERT INTO TripSets 
(Date, UserId)
VALUES     
(@Date,@UserId);
SELECT @TripId = SCOPE_IDENTITY()

也许您会收到解析器错误警告,但您可以忽略它。现在配置完成后,我们就可以在业务逻辑代码中使用,如下所示:

int tripId;        
int result = tripSetsTableAdapter.Insert(tripDate, userId, out tripId);

// Here comes the insert method into the second table

tripSetTripSearchTableAdapter.Insert(tripId, amountPersons);

您可能希望以某种方式同步此操作(例如使用 TransactionScope) 但这完全取决于您。

Today it is so quiet here... Ok if anybody is also looking for such a solution, I've found a way to do it.

So our main problem is to get the id of the newly created record in the first table. If we're able to do that, after that we simply supply it to the next method which creates a corresponding record in the second table.

I used a DataSet Designer in order to enjoy the code autogeneration feature of the VS. Let's call the first table TripSets. In DataSet Designer right click on the TripSetsTableAdapter, then Properties. Expand InsertCommand properties group. Here we need to do two things.

First we add a new parameter into the collection of parameters using the Parameters Collection Editor. Set ParameterName = @TripId, DbType = Int32 (or whatever you need), Direction = Output.

Second we modify the CommandText (using Query Builder for convenience). Add to the end of the command another one after a semicolon like that:

(...);
SELECT @TripId = SCOPE_IDENTITY()

So you will get something like this statement:

INSERT INTO TripSets 
(Date, UserId)
VALUES     
(@Date,@UserId);
SELECT @TripId = SCOPE_IDENTITY()

Perhaps you will get a parser error warning, but you can just ignore it. Having this configured now we are able to use in our Business logic code as follows:

int tripId;        
int result = tripSetsTableAdapter.Insert(tripDate, userId, out tripId);

// Here comes the insert method into the second table

tripSetTripSearchTableAdapter.Insert(tripId, amountPersons);

Probably you will want to synchronize this operations somehow (e.g. using TransactionScope) but it is completely up to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文