使用asp.net从sql server中的三个表中获取记录 [英] Fetching record from three tables in sql server using asp.net

查看:68
本文介绍了使用asp.net从sql server中的三个表中获取记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在我的项目中我有三张桌子

调查(SurveyID,标题,描述)
问题(QuestionID,SurveyID,Questiontext)
选择(ChoiceId,QuestionID,Choicetext)



现在我想显示问题及其选项基于调查标题?

i有一个调查标题下拉列表我想选择调查标题,其选项的问题应显示在数据网格上。 />
i想要做这样的查询

从选项中选择选择文本问题ID =从问题文本中选择问题ID =从问题中选择问题文本其中Surveytitle = dropdownlist.selectedvalue



i我没有在sql server中执行此操作的确切想法。

如何进行Sql查询这个?

Plz帮助我... ...

解决方案

如果下拉列表绑定如下

< pre lang =HTML> dropdownlist.DataSourc e = dtServey
dropdownlist.DisplayMember =Title
dropdownlist.ValueMember =SurveyID
dropdownlist.Databind();



查询应如下所示

 选择 choicetext 
选择c
INNER JOIN 问题q
ON q.QuestionID = c.QuestionID
INNER 加入调查s
ON s.SurveyID = q.SurveyID
其中 s.SurveyID = @ SurveyID



下拉列表选择更改事件

传递@ SurveyID参数值dropdownlist.selectedvalue



快乐编码!

:)


•设置时dropdownlist source,设置dropDown List.DataTextField = Title和dropDownList.DataValueField = SurveyID;

•创建一个可以保存所选值的sql参数(调查ID)

•加入问题和选择表基于调查ID

•根据QuestionID使用GroupBy





了解更多关于Joins,sql参数的信息&查询


您好,



您可以编写如下所示的查询:

 SELECT * 
FROM table_1
JOIN table_2 ON(table_2.table_1_id = table_1.table_1_id)
JOIN table_3 ON(table_3.table_1_id = table_1.table_1_id)





问候,

Shree M。



in my project i have three tables

Survey(SurveyID,Title,Description)
Question(QuestionID,SurveyID,Questiontext)
Choices(ChoiceId,QuestionID,Choicetext)


Now i want to show question and its option bases on the survey title?
i have a dropdownlist of survey titles i want to select survey title and questions with its options should be shown on datagrid.
i want to do some query like this

Select choicetext from Choices Where Question ID ="Select QuestionID From Questions Where Questiontext="Select Questiontext From Questions Where Surveytitle=dropdownlist.selectedvalue""


i am not geting the exact idea hoe to do this in sql server.
How to make Sql Query of this?
Plz Help ME Thnks...

解决方案

if dropdownlist is bind as below

dropdownlist.DataSource = dtServey
dropdownlist.DisplayMember = "Title"
dropdownlist.ValueMember = "SurveyID"
dropdownlist.Databind();


Query should be like below

Select choicetext 
from Choices c
INNER JOIN Questions q
ON q.QuestionID = c.QuestionID
INNER JOIN Survey s
ON s.SurveyID = q.SurveyID
Where s.SurveyID = @SurveyID


dropdownlist selection change event
pass @SurveyID parameter value dropdownlist.selectedvalue

Happy Coding!
:)


• While setting the dropdownlist source, set the dropDownList.DataTextField = Title and dropDownList.DataValueField = SurveyID;
• Create a sql parameter which can hold the selected value(survey ID)
• Join the Questions & Choices table based on the survey ID
• Use GroupBy based on QuestionID


Read more about Joins, sql parameters & queries


Hi,

You can write your query as shown below:

SELECT *
FROM table_1
JOIN table_2 ON (table_2.table_1_id = table_1.table_1_id)
JOIN table_3 ON (table_3.table_1_id = table_1.table_1_id)



Regards,
Shree M.


这篇关于使用asp.net从sql server中的三个表中获取记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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