[英]How to preform search operation from combobox using data/value member in Visual Studio 2012 C#
I am at beginner level at this kind of programming so I believe that someone of you will know how to resolve this problem I'm having. 我是这类编程的初学者,所以我相信你们中的某人会知道如何解决我遇到的这个问题。
I have a small project in Visual Studio that is connected with Oracle Database 11g Express, and I want to perform a search operation on particular table from database and display it at this form in my Visual Studio project. 我在Visual Studio中有一个与Oracle Database 11g Express连接的小项目,我想对数据库中的特定表执行搜索操作,并将其以这种形式显示在我的Visual Studio项目中。 So I have two comboboxes filled with Displayed Members over Data Members.
因此,我有两个组合框,其中填充了“显示成员”而不是“数据成员”。 Data members are usually foreign keys in tables, such as ID etc., and display members that are showing in these comboboxes are Name and Surname usually, of course connected with those IDs I mention before.
数据成员通常是表中的外键,例如ID等,这些组合框中显示的显示成员通常是Name和Surname,当然,它们与我之前提到的ID有关。
So I want to select these Display Members and over them to search all the data from particular at. 因此,我想选择这些展示成员并在其上方搜索特定位置的所有数据。
This is a query for select button in my form: 这是我的表单中对选择按钮的查询:
OracleConnection con = new OracleConnection("DATA SOURCE=localhost:1521/XE;PERSIST SECURITY INFO=True;USER ID=BAYE;PASSWORD=blagojica");
con.Open();
OracleDataAdapter dr = new OracleDataAdapter("SELECT * FROM zaposlenja where RAD_JMBG='" + comboBox1.SelectedValue + "' or ODELJENJEID='"+comboBox2.SelectedValue+"'", con);
DataTable dt = new DataTable();
dr.Fill(dt);
dataGridView2.DataSource = dt;
con.Close();
this.dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;`
And I have this table, where ime_pacijenta
is name, prezime_pacijenta
is surname, and PRETRAGA
is this search button: 我有此表,其中
ime_pacijenta
是名称, prezime_pacijenta
是姓氏, PRETRAGA
是此搜索按钮:
And when I select one item from combobox it should write out just that one record. 当我从组合框中选择一项时,它应该只写出一条记录。 Instead it shows me another record that has nothing to do with this one.
相反,它向我显示了另一条与此无关的记录。 It also happens when I select any other value from combobox:
当我从组合框中选择任何其他值时,也会发生这种情况:
So that would be it, I appreciate any reply! 就是这样,我感谢您的答复!
I think the problem is caused by the WHERE
statement of your SELECT
query: 我认为问题是由
SELECT
查询的WHERE
语句引起的:
OracleDataAdapter dr = new OracleDataAdapter("SELECT PACIJENT_JMBG, IME_PACIJENTA, PREZIME_PACIJENTA, NAZIV, OPIS, NAZIVDIJAGNOZE, DIJAGNOZAID FROM TERAPIJE WHERE PACIJENT_JMBG='"+comboBox1.SelectedValue+"' or DIJAGNOZAID='"+comboBox2.SelectedValue+"'", con);
You should not include in your WHERE
statement any combox that doesn't have a value. 您不应该在
WHERE
语句中包含任何没有值的combox。 In the image you are showing; 在图像中显示; the second combobox doesn't have a value but it's still considered in the query, which in turn will affect the result.
第二个组合框没有值,但查询中仍会考虑它,这反过来会影响结果。 You should rewrite your
WHERE
statement part if at least one of the comboboxes doesn't have a value or you can just make your comboboxes have values by default. 如果至少一个组合框没有值,或者您可以默认使组合框具有值,则应该重写
WHERE
语句部分。
NOTE: You should be sure whether you want to use ORing or ANDing in the WHERE
statement, since using ORing may return you multiple records. 注意:您应该确定要在
WHERE
语句中使用ORing还是ANDing,因为使用ORing可能会返回多个记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.