在C#中从数据库中选择多个值 [英] Selecting multiple values from database in C#

查看:61
本文介绍了在C#中从数据库中选择多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:我有一个包含以下食品的清单:大米,肉,香蕉.假设所有三个项目均已检查.是否可以在单个select语句中从数据库中检索这些选中项的id,并将选中项作为select语句的dbparameter值.

[edit]仅主题-OriginalGriff [/edit]

Scenario: I have a checkedlistbox with the following food items: rice, meat, bananas. Lets assume all three items have been checked. Is it possible to retrieve the ids of these checked items from database in a single select statement and the checked items as dbparameter values for select statement.

[edit]Subject only - OriginalGriff[/edit]

推荐答案

假设您有可变数量的参数(即,可能不止三个!),然后简短的回答是,它是可能的,并且有很多方法可以实现.

您可以将字符串描述连接成单个逗号分隔的字符串,将其作为参数传递,然后将其传递给存储过程,该存储过程返回一个表,每个表的ID为Id

您可以在SQL Server中使用用户定义的类型,将其填充到应用程序中,并将其作为单个参数传递给存储过程,然后再次返回Ids

但是,我想知道您是从哪里获得字符串s的,它们不是最初从数据库中获得的吗?如果是这样,那么当您检索字符串时,也要检索它们的ID,并将它们存储在您的应用程序中,因此以后无需再进行检索
Assuming you have a variable number of parameters (i.e. there may be more than just the three!) then the short answer is that it is possble and there are a number of ways to do it.

you COULD concatenate the string descriptions into a single comma separated string, pass that as a parameter and pass it to a stored procedure that returns a table with one row per Id

you COULD use a user-defined type in SQL Server, populate it in your application and pass it as a single parameter to a stored procedure, and again return the Ids

however, I wonder where you got the string s from - aren''t they from teh database originally? if so, then when you retrieve the strings, retrieve their Ids too,and store them in your application, so no need to go retrieve them later


这篇关于在C#中从数据库中选择多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆