使用 ASP.NET C# 在 Microsoft SQL 中添加/更新/删除查找表的最佳方法是什么?

发布于 2024-10-14 21:48:08 字数 1136 浏览 4 评论 0原文

我正在开发一个本地城市项目,对如何在 Microsoft SQL 2000 中有效地创建“公园”和“活动”之间的关系有一些疑问。我们使用 ASP.NET C# 来

创建两个表“公园”和“活动”。 ”我还创建了一个查找表,其中在“公园”和“活动”的主键上设置了正确的关系。我的查找表称为“ParksActivities”。

我们有大约 30 项活动可以与每个公园相关联。一名实习生将负责管理该网站,并且每 6 个月将对活动进行评估。

到目前为止,我已经创建了一个管理工具,允许您添加/编辑/删除每个公园。添加公园很简单。数据是新的,所以我只是允许他们编辑公园详细信息,并关联从数据库动态提取的“活动”。这是在中继器控件中完成的。

编辑是有效的,但我不认为它的效率有多高。保存主要公园详细信息没有问题,因为我只需在创建的公园实例上调用 Save() 即可。但是,要删除查找表中的过时记录,我只需DELETE FROM ParksActivities WHERE ParkID = @ParkID”,然后为每个已检查的活动INSERT一条记录。

对于我的查找表上的 ID 列,我有一个递增的整数值,经过相当多的测试后,该值已达到数千。虽然这确实有效,但我认为必须有更好的方法来更新查找表

。提供一些关于如何改进这一点的见解?我目前正在使用存储过程,但我不是最擅长非常复杂的语句,

[ParkID | ParkName | Latitude | Longitude ]  
 1      | Freemont | -116.34  | 35.32  
 2      | Jackson  | -116.78  | 34.2    


[ActivityID | ActivityName | Description ]
1           | Picnic       | Blah
2           | Dancing      | Blah
3           | Water Polo   | Blah

[ID | ParkID | ActivityID ]
 1  | 1      | 2
 2  | 2      | 1
 3  | 2      | 2
 4  | 2      | 3

我更愿意学习如何以更通用的方式来实现它,而不是使用 Linq-To-。 SQL 或 ADO.NET。

I'm working on a local city project and have some questions on efficiently creating relationships between "parks" and "activities" in Microsoft SQL 2000. We are using ASP.NET C# to

I have my two tables "Parks" and "Activities." I have also created a lookup table with the proper relationships set on the primary keys of both "Parks" and "Activities." My lookup table is called "ParksActitivies."

We have about 30 activities that we can associate with each park. An intern is going to be managing the website, and the activities will be evaluated every 6 months.

So far I have created an admin tool that allows you to add/edit/delete each park. Adding a park is simple. The data is new, so I simply allow them to edit the park details, and associate "Activities" dynamically pulled from the database. This was done in a repeater control.

Editing works, but I don't feel that its as efficient as it could be. Saving the main park details is no problem, as I simply call Save() on the park instance that I created. However, to remove the stale records in the lookup table I simply DELETE FROM ParksActitivies WHERE ParkID = @ParkID" and then INSERT a record for each of the checked activities.

For my ID column on the lookup table, I have an incrementing integer value, which after quite a bit of testing has got into the thousands. While this does work, I feel that there has to be a better way to update the lookup table.

Can anyone offer some insight on how I may improve this? I am currently using stored procedures, but I'm not the best at very complex statements.

[ParkID | ParkName | Latitude | Longitude ]  
 1      | Freemont | -116.34  | 35.32  
 2      | Jackson  | -116.78  | 34.2    


[ActivityID | ActivityName | Description ]
1           | Picnic       | Blah
2           | Dancing      | Blah
3           | Water Polo   | Blah

[ID | ParkID | ActivityID ]
 1  | 1      | 2
 2  | 2      | 1
 3  | 2      | 2
 4  | 2      | 3

I would prefer to learn how to do it a more universal way as opposed to using Linq-To-SQL or ADO.NET.

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

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

发布评论

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

评论(2

身边 2024-10-21 21:48:08

与使用 LINQ2SQL 或 ADO.NET 相比,更愿意学习如何以更通用的方式进行操作。

显然您正在使用 ADO.NET Core :)。这很好,我认为您应该坚持使用存储过程和 DbCommands 等...

如果您使用 MSSQL 2008,您将能够使用 TableValued 参数和 MERGE 语句来完成此操作。既然您使用的是 MSSQL 200(为什么?),您需要执行以下操作:
1. 将逗号分隔的 Activity id(新的)列表与 ParkId 一起发送到您的存储过程。例如,ActivityIds 参数是 varchar(50)。

在您的存储过程中,您可以拆分 ids

该策略类似于
1. 对于传入的Id,删除不匹配的记录

SQL 为

DELETE FROM ParkActivities
WHERE ActivityId NOT IN(一些 ID 列表)
WHERE ParkId = @ParkId

由于您的列表是一个字符串,您可以像这样执行

EXEC('DELETE FROM ParkActivities WHERE ActivityId NOT IN (' + @ActivityIds + ') AND ParkId = ' + @ParkId)

  1. 现在您可以插入那些活动尚未在表中。最简单的方法是将 ParkActivity id 插入到临时表中。为此,您需要将逗号分隔的列表拆分为单独的 ID,并将它们插入到临时表中。一旦临时表中有数据,您就可以插入进行连接。

这是 MSSQL 2000 中的内置用户定义函数,可以执行拆分并返回一个表变量,其中每个值位于单独的行上。
http://msdn.microsoft.com/en-us/library/Aa496058

would prefer to learn how to do it a more universal way as opposed to using LINQ2SQL or ADO.NET.

You're obviously using ADO.NET Core :). And that's fine I think you should stick to using Stored procedures and DbCommands and such...

If you were using MSSQL 2008 you'd be able to do this using TableValued parameters and the MERGE statement. since you're using MSSQL 200 (why?) what you'd need to do is the following:
1. Send a comma delimited list of the Activity ids (the new ones) along with the ParkId to your stored proc. The ActivityIds parameter would be a varchar(50) for example.

In your stored proc you can split the ids

The strategy would be something like
1. For the Ids passed in, delete records that don't match

The SQL for that would be

DELETE FROM ParkActivities
WHERE ActivityId NOT IN (Some List of Ids)
WHERE ParkId = @ParkId

Since your list is a string you can do it like this

EXEC('DELETE FROM ParkActivities WHERE ActivityId NOT IN (' + @ActivityIds + ') AND ParkId = ' + @ParkId)

  1. Now you can insert those activities that are not already in the table. The simplest way to do this would be to insert the ParkActivity ids into a temp table. To do that you'll need to split the comma delimited list into individual ids and insert them into a temp table. Once you have the data in the temp table you can insert doing a join.

The is a built-in user defined function in MSSQL 2000 that can do the split and return a Table Variable with each value on a seperate row.
http://msdn.microsoft.com/en-us/library/Aa496058

明天过后 2024-10-21 21:48:08

LinqToSQL 和 ADO.NET 有什么问题?我的意思是,您能否具体说明您对使用这些技术

更新的

疑虑,如果 2000 不支持 LinqToSQL,您可以轻松升级到免费的 2008 Express。对于您所描述的目的来说,这绝对足够了。

What is wrong with LinqToSQL and ADO.NET? I mean, could you specify your doubts about using those technologies

update

if LinqToSQL is not supported for 2000, you can easily upgrade to free 2008 express. It would be definitely enough for purposes you described.

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