检索单个数据 [英] Retrieve a single data

查看:73
本文介绍了检索单个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个Windows窗体应用程序。有许多形式可用于数据操作。在Sales_Details的形式中,我在数据库中输入医生的信息,例如医生姓名,地址,移动等。在另一个表格中,我使用了一个comboBox控件作为医生姓名,我从Sales_Details中检索医生名称到这个组合框,其中医生姓名第一次进入。我为此编写了这段代码

 DataTable dt2 =  new  DataTable(); 
SqlConnection cn1 = new SqlConnection(Class1.cs);
SqlDataAdapter da2 = new SqlDataAdapter( select来自Sales_Details的Doctor_Name,cn1);
da2.Fill(dt2);
comboBox2.DataSource = new BindingSource(dt2, null );
comboBox2.DisplayMember = Doctor_Name;
comboBox2.Text = 选择一个......;
cn1.Close();



当我运行此代码时它运行正常但它检索并在comboBox中显示一个特定的医生名称5次如果我使用第一个表格输入该特定医生姓名5次; Sales_Details。我想只显示一次特定的医生姓名,无论我是否输入5或6次。请有人帮忙。

我用C#和SQL Server 2005来开发这个应用程序。

解决方案

 < span class =code-keyword>从 Sales_Details  

但不是一个完美的解决方案,因为两个医生可以有相同的名字。


你应该为每个医生都有一个代码。我建议你在DropDown中显示代码和医生姓名。试试这个:

 DataTable dt2 =  new  DataTable(); 
SqlConnection cn1 = new SqlConnection(Class1.cs);
SqlDataAdapter da2 = new SqlDataAdapter( SELECT (Doctor_Name +' - '+ Doctor_Code)AS Doctor_Name,Doctor_Code FROM Sales_Details,cn1);
da2.Fill(dt2);
comboBox2.DataSource = new BindingSource(dt2, null );
comboBox2.DisplayMember = Doctor_Name;
comboBox2.Text = 选择一个......;
cn1.Close();



现在,如果两位医生的姓名相同,那么用户可以轻松识别他们的代码。



--Amit


_Amy是对的我的解决方案是修改后的形式

 DataTable dt2 =  new  DataTable(); 
SqlConnection cn1 = new SqlConnection(Class1.cs);
SqlDataAdapter da2 = new SqlDataAdapter( SELECT Doctor_Code,(Doctor_Name +' - '+ Doctor_Code)AS Doctor_Name,Doctor_Code FROM Sales_Details,cn1);
da2.Fill(dt2);
comboBox2.DataSource = new BindingSource(dt2, null );
comboBox2.DisplayMember = Doctor_Name;
comboBox2.ValueMember = Doctor_Code;
comboBox2.Text = 选择一个......;
cn1.Close();





现在你可以用comboBox.SelectedValue属性获得实际的医生代码。


I developed a Windows Form Application. There are many forms available for data manipulation. In a form namely Sales_Details I enter information of a doctor in database such as Doctor Name, Address, Mobile etc. In one another form I used a comboBox control for Doctor Name, I retrieve the Doctor Name to this comboBox from Sales_Details where the Doctor Name entered first time. I wrote this code for that

DataTable dt2 = new DataTable();
SqlConnection cn1 = new SqlConnection(Class1.cs);
SqlDataAdapter da2 = new SqlDataAdapter("select Doctor_Name from Sales_Details", cn1);
da2.Fill(dt2);
comboBox2.DataSource = new BindingSource(dt2, null);
comboBox2.DisplayMember = "Doctor_Name";
comboBox2.Text = "Select One...";
cn1.Close();


while I run this code it''s running properly but it retrieves and shows in the comboBox a particular Doctor Name 5 times if I entered that particular Doctor Name 5 times by using the first form viz; Sales_Details. I want to show that particular Doctor Name only one time whether I entered it 5 or 6 times no matter. Please someone help.
I used C# and SQL Server 2005 to develop this application.

解决方案

select DISTINCT Doctor_Name from Sales_Details


But not a perfect solutions since two doctors can have the same name.


You should have a code for each doctors. I would suggest you to display code along with the doctor names in DropDown. Try this:

DataTable dt2 = new DataTable();
SqlConnection cn1 = new SqlConnection(Class1.cs);
SqlDataAdapter da2 = new SqlDataAdapter("SELECT (Doctor_Name +' - '+ Doctor_Code) AS Doctor_Name, Doctor_Code  FROM Sales_Details", cn1);
da2.Fill(dt2);
comboBox2.DataSource = new BindingSource(dt2, null);
comboBox2.DisplayMember = "Doctor_Name";
comboBox2.Text = "Select One...";
cn1.Close();


Now, if the two doctors are having same name also then user can easily identify with their codes.

--Amit


_Amy is right my solution is lite modified form

DataTable dt2 = new DataTable();
SqlConnection cn1 = new SqlConnection(Class1.cs);
SqlDataAdapter da2 = new SqlDataAdapter("SELECT Doctor_Code, (Doctor_Name +' - '+ Doctor_Code) AS Doctor_Name, Doctor_Code  FROM Sales_Details", cn1);
da2.Fill(dt2);
comboBox2.DataSource = new BindingSource(dt2, null);
comboBox2.DisplayMember = "Doctor_Name";
comboBox2.ValueMember = "Doctor_Code";
comboBox2.Text = "Select One...";
cn1.Close();



now you can get the actual doctor code with comboBox.SelectedValue property.


这篇关于检索单个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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