2个数据库表,1个组合框选择字段,用所选数据填充datagridview [英] 2 database tables, 1 combo box select field to populate datagridview with selected data
问题描述
您好,
我正在开发一个简单的小程序,只是为了更好地完成一些事情并更好地理解一些代码。它涉及c#和SQL。
我有2个数据库表,它们有一个ORDERID的公共字段。我的C#程序中有2个控件,一个组合框和一个数据网格视图。我有一个组合框填充了其中一个表的所有orderid,我希望我的程序执行此操作。
当我在组合框中选择一个有序时,我想用所有的数据库填充datagridview另一张桌子的特定顺序。
这就是我现在所拥有的,我想知道我是否可以从这个简单的小任务中得到帮助。
// 这会填充组合框,其中包含1个表订单ID idk,如果这是正确的方式
public partial 类 MainWindow:窗口
{
public static < span class =code-keyword> string connectionString = @ 数据源=(LocalDB)\ V11.0; AttachDbFilename = | DataDirectory目录| \Centu rionDB.mdf; Integrated Security = True;
public MainWindow()
{
InitializeComponent();
使用(SqlConnection sqlcon = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand( SELECT * FROM [dbo]。[Orders],sqlcon);
sqlcon.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
ddlOrders.Items.Add(reader [ OrderID]。ToString());
}
reader.Close();
}
}
// 我的数据库数据
INSERT INTO [dbo]。[Orders](OrderID,Type,Price)VALUES( 1111 ,' gG', 1 。 1 )
INSERT INTO [dbo]。[Orders](OrderID,Type,Price)VALUES( 2222 ,' BP', 1 。 2 )
INSERT INTO [dbo]。[Orders](OrderID,Type,Price)VALUES( 3333 ,' gG', 1 。 3 )
INSERT INTO [dbo]。[buy](buysID,OrdersID,item,Type)VALUES( 121 , 1111 , ' 监控',' gG')
INSERT INTO [dbo]。[buys](buysID,OrdersID,item,Type)VALUES( 122 , 1111 ,' 键盘',' gG')
INSERT INTO [dbo]。[buys](buysID, OrdersID,item,Type)VALUES( 131 , 2222 , ' TV',' BP')
INSERT INTO [dbo]。[buys](buysID,OrdersID,item,Type)VALUES( 132 , 2222 ,' Speakers',' BP')
INSERT INTO [dbo]。[buys](buysID,OrdersID,item,Type)VALUES( 141 , 3333 ,' sk8', ' sk8')
INSERT INTO [dbo]。[buys](buysID,OrdersID, item,Type)VALUES( 142 , 3333 ,' bearing',' sk8')
在组合框的点击事件中试用此代码
尝试 {
SqlCommand cmd1 = new SqlCommand(< span class =code-string> 从购买中选择*,其中OrdersID = + ddlOrders.SelectedValue + ,sqlcon);
sqlcon.Open();
SqlDataReader readBuys = cmd1.ExecuteReader();
while (readBuys.Read())
{
datagridview1.Clear();
datagridview1.DataSource = readbuys;
}
}
catch (Exception exp)
{
}
Hello,
I hve a small simple program I am working on just to get better at a few things and understand a little bit of code better. Its involving c# and SQL.
I have 2 database tables and they have a common field which is ORDERID. I have 2 controls in my C# program a combo box and a datagrid view. I have the combo box populated with all the orderids of one of the tables and I want my program to do this.
When I select a ordered in the combo box I want to populate the datagridview with all the specific orderids of the other table.
This is what I currently have and I was wondering if I can get help with this small simple task.
//this populates the combo box with 1 tables order ids idk if this is the right way tho
public partial class MainWindow : Window
{
public static string connectionString= @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\CenturionDB.mdf;Integrated Security=True";
public MainWindow()
{
InitializeComponent();
using (SqlConnection sqlcon = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand("SELECT * FROM [dbo].[Orders]", sqlcon);
sqlcon.Open();
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
ddlOrders.Items.Add(reader["OrderID"].ToString());
}
reader.Close();
}
}
//my database data
INSERT INTO [dbo].[Orders](OrderID, Type, Price) VALUES(1111, 'gG', 1.1)
INSERT INTO [dbo].[Orders](OrderID, Type, Price) VALUES(2222, 'BP', 1.2)
INSERT INTO [dbo].[Orders](OrderID, Type, Price) VALUES(3333, 'gG', 1.3)
INSERT INTO [dbo].[buys]( buysID, OrdersID, item, Type) VALUES(121, 1111, 'Monitor', 'gG')
INSERT INTO [dbo].[buys]( buysID, OrdersID, item, Type) VALUES(122, 1111, 'Keyboard', 'gG')
INSERT INTO [dbo].[buys]( buysID, OrdersID, item, Type) VALUES(131, 2222, 'TV', 'BP')
INSERT INTO [dbo].[buys]( buysID, OrdersID, item, Type) VALUES(132, 2222, 'Speakers', 'BP')
INSERT INTO [dbo].[buys]( buysID, OrdersID, item, Type) VALUES(141, 3333, 'sk8', 'sk8')
INSERT INTO [dbo].[buys]( buysID, OrdersID, item, Type) VALUES(142, 3333, 'bearings', 'sk8')
Try this code at the click event of combobox
try{ SqlCommand cmd1=new SqlCommand("Select * from buys where OrdersID="+ddlOrders.SelectedValue+"",sqlcon); sqlcon.Open(); SqlDataReader readBuys = cmd1.ExecuteReader(); while(readBuys.Read()) { datagridview1.Clear(); datagridview1.DataSource=readbuys; } } catch(Exception exp) { }
这篇关于2个数据库表,1个组合框选择字段,用所选数据填充datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!