在文本框C#中显示查询的返回值 [英] displaying return value of query in textbox C#
问题描述
我是C#的新手.
我在显示时遇到问题,在一个文本框中,我的查询返回值在下面给出了这个问题的详细信息.
这是我的桌子.
table_name-时间表
I am newbie to C#.
I have some problem in displaying, In a textbox my query return value i give this problen detail below .
This is my table.
table_name - Timetable
Grade | div | week | period | subject | teacher
10 | A | mon | 1 | sci | john
11 | E | tue | 4 | ICT | anton
这是我的查询.
This is my query.
SELECT subject
FROM Timetable
WHERE Grade=@grade AND div='@div' AND Period= 1 AND week=mon
在C#中,我具有Gradecombobox (6,7,8,9,10,11)
Divisioncombobox (A,B,C,D)
,我可以将等级和除法作为参数传递,并且当应该在textbox1中单击单击按钮时应显示返回值.
示例-如果选择10年级和div A"sci"应该显示在文本框中,请期待此方法的c#完整编码.
请帮帮我.
[edit]已添加代码块-OriginalGriff [/edit]
In C# I have Gradecombobox (6,7,8,9,10,11)
Divisioncombobox (A,B,C,D)
, I can pass grade and division as parameter and when click button in textbox1 returnvalue should be displayed.
Example- if select grade 10 and div A "sci" should be disply in textbox please I expect c# full coding of this method .
Please help me.
[edit]Code block added - OriginalGriff[/edit]
推荐答案
@div不应为单格("@div")标记.这是代码:
Hi,
@div shouldn''t be in single cotation (''@div'') mark. here''s the code :
SqlCommand cmd = Connection.CreateCommand();
cmd.CommandText = @"SELECT subject
FROM Timetable
WHERE Grade=@grade AND div=@div AND Period= 1 AND week=mon";
cmd.Parameters.AddWithValue("@grade",Gradecombobox.SelectedValue);
cmd.Parameters.AddWithValue("@div ",Divisioncombobox.SelectedValue);
try
{
Connection.Open();
object result = cmd.ExecuteScalar();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
祝您好运,如果可以解决您的问题,请记住将其标记为答案;如果没有提供更多详细信息.
Good luck , remember to mark as an answer if it solves your problem; If not give us more details .
这篇关于在文本框C#中显示查询的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!