使用 ASP.NET C# 在 Microsoft SQL 中添加/更新/删除查找表的最佳方法是什么?
我正在开发一个本地城市项目,对如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
显然您正在使用 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)
这是 MSSQL 2000 中的内置用户定义函数,可以执行拆分并返回一个表变量,其中每个值位于单独的行上。
http://msdn.microsoft.com/en-us/library/Aa496058
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)
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
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.