简体   繁体   English

如何从SQL数据库中获取数据到组合框 - C#

[英]How to fetch data from SQL database to combo box - C#

I have a radio button & combo box like this: 我有一个这样的单选按钮和组合框:

在此输入图像描述

If the user chooses the radio button either single or double, the combo box automatically fetch the data (room number which is single-type if I choose single-type). 如果用户选择单个或双重单选按钮,组合框会自动获取数据(如果选择单一类型,则为单一类型的房间号)。 I already have stored procedure like this. 我已经有这样的存储过程了。

create procedure viewRoom
@IDBranch varchar(5),
@NoTypeRoom varchar(5)

as

select NoRoom
from MsRoom
where NoTypeRoom = @NoTypeRoom
and IDBranch = @IDBranch
and AvailabilityRoom>0 

What should I do, so the fetched data can be shown in the combo box every time I choose the radio button? 我该怎么办,所以每次选择单选按钮时,获取的数据都会显示在组合框中?

you will have to use cmd.CommandType=CommandType.StoredProcedure 您将不得不使用cmd.CommandType=CommandType.StoredProcedure

SqlCommand cmd  = new SqlCommand("viewRoom", conn);
cmd.CommandType=CommandType.StoredProcedure;
da=new SqlDataAdapter(cmd);
ds=new DataSet();
da.Fill(ds);
for(int i=0;i<ds.Tables[0].Rows.Count;i++)
cmbItems.items.add(ds.Tables[0].Rows[i][0].tostring()); //way to put table in combo

Try to use this kind of code. 尝试使用这种代码。

Hope this helps you. 希望这对你有所帮助。

You can Populate Combobox like this: 你可以像这样填充Combobox:

comboBox1.DisplayMember = "YourColmName";
comboBox1.ValueMember = "YourFied"; //Field which you want set the value of combobox 
comboBox1.DataSource = PopulateCombo(NoTypeRoom,IDBranch) // variables

Here in this case may be your Value and label are same, but in other case you need to specify 在这种情况下,您的值和标签可能相同,但在其他情况下您需要指定


Public DataTable PopulateCombo(string NoTypeRoom,string IDBranch)
{
SqlCommand cmd = new SqlCommand("viewRoom", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@NoTypeRoom", NoTypeRoom);
cmd.Parameters.AddWithValue("@IDBranch", IDBranch);
SqlDataAdapter dap = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
dap.Fill(ds);
return ds.Tables[0];  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM