开发者

Adding a list of tags (from UI) into database?

I have this food recipe asp.net c# application I'm working on and I would like to be able to insert a list of tags into the database for each recipe. So let's say I have three tags (southern, cajun, gumbo) that correspond开发者_StackOverflow to a gumbo recipe. How would I insert these three tags into the database from the UI? Would I store them in a List, run a foreach, and insert each tag individually? I'd really like to be able to somehow use a stored procedure to do this. I hope this was enough information to help. Thanks.

BTW - the database is already design, I just need help inserting the list into a table.


You say the database is already designed, but answers to this question might still affect the design. I mean, how do you save the tags, do you have a table with one column for recipe name and another with datatype varchar(500) for example where you store the tags. In this case you can write a stored procedure which takes recipe and tags as argument and save it in the required table. Another design might be one that involves a second table with fields recipe_name and tag in which for each recipe there are several tags. In this design you can also use the above mentined stored procedure. You can send in the tags as a comma separated string to the procedure and inside the procedure split the string into tags and save them to the table. Each design decision has own advantages and disadvantages and various levels of technical stuff when facing tasks such as searching for tags and etc. Personally i would prefer the second method for larger applications as it provides more means of reporting in the end.


This is a design decision, so different people will have different ideas, but these are mine:

I would create a stored procedure to insert one tag at a time. Don't use SQL Server to work your for-each loops - it's not what it's made for.

So, in the layer that makes the database call (this might be your application, or a middle layer such as a webservice) have a method that takes a List(Of RecipeTag), creates the connection and then loops through each RecipeTag calling the stored procedure. (you can use for-each with Lists as they are enumerable). If you do this then you aren't opening and closing a database connection each time, just at the start and end, so it'll be fast.

RecipeTag (in my example) is some object that you've created that your front end can use to keep track of any other information (such as who added it, what recipe it's for, etc) in a nice discrete container without worrying about how it's going to be dealt with. You can add extra methods into this object to deal with validation etc if you wish.

With the above design you have: a database level that doesn't know or care about multiple objects, it just has a stored procedure to insert a set of values, and a front-end that can deal with a collection of strongly-typed objects, and a middle layer (or maybe a class in your front-end app) that does that translation between the two.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜