简体   繁体   English

如何在Visual Studio 2012 C#中使用数据/值成员从组合框执行搜索操作

[英]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.

相关问题 Visual Studio 2012 Modernappstyle可编辑组合框C# - Visual Studio 2012 Modernappstyle Editable Combobox c# 如何在Visual Studio 2012 C#上更新我的数据源? - How to update my data sources on visual studio 2012 c#? C#:如何在Visual Studio 2012中将诸如标签或文本框值之类的文本从窗体发送到Crystal报表? - C#: How to send text like label or textbox value from form to crystal reports in visual studio 2012? 如何使用SQL Server 2012从C#中的多个表中搜索数据 - How to search a data from multiple tables in C# using SQL Server 2012 如何防止Visual Studio 2012重命名成员类型(使用Entity Framework)? - How to prevent Visual Studio 2012 from renaming member types (using Entity Framework)? 清除ListBox中的数据并在Visual Studio 2012 C#XAML中重新加载 - Clearing Data in ListBox and Reload in Visual Studio 2012 C# XAML 在Visual Studio 2012 C#中使用由Eclipse制作的C ++ DLL? - Using c++ dll made from eclipse in visual studio 2012 c#? 使用C#,WPF,实体框架和Visual Studio 2012不会在数据库中更新数据 - Data is not updated in database, using C#,WPF, Entity framework and Visual studio 2012 C#奇怪的ComboBox数据-Visual Studio问题? - C# Strange ComboBox Data — Visual Studio Issue? 使用Visual Studio 2012 C#的WPF中的DataGrid示例 - DataGrid sample in WPF using visual studio 2012 C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM