C# 在文本框中显示相关数据的C数据库

C# 在文本框中显示相关数据的C数据库,c#,C#,我遇到了麻烦,如果有人能帮我,我将不胜感激 我有一个简单的MS Access数据库,它链接到我的程序。我做的第一件事是用数据库产品描述中的一个字段填充一个组合框 我想做的是,当用户在组合框中选择一个项目时,记录中的所有其他字段都将显示在文本框中 string sConnection; sConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=XYZDataba

我遇到了麻烦,如果有人能帮我,我将不胜感激

我有一个简单的MS Access数据库,它链接到我的程序。我做的第一件事是用数据库产品描述中的一个字段填充一个组合框

我想做的是,当用户在组合框中选择一个项目时,记录中的所有其他字段都将显示在文本框中

string sConnection;
        sConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" +
                        "Data Source=XYZDatabase.accdb";
        OleDbConnection dbConn;
        dbConn = new OleDbConnection(sConnection);
        dbConn.Open();
        cbxProducts.DisplayMember = "Product Description";
        dbConn.Close();
我考虑过可能使用SQL或DataReader,尽管我真的不确定。 这是我希望填充文本框的事件。我真的被困在这里了

private void cbxProducts_SelectedIndexChanged(object sender, EventArgs e)
    {          
        txtProductNumber.Text = 
        txtProductDescription.Text = 
        txtProductCategory.Text = 
        txtProductCost.Text = 
    }

我希望我的问题没有格式错误或其他什么,如果我有,道歉,这是我第一次在这里发布!Dx

我想知道您的组合框是否实际显示了数据库中的数据

在之后的第一个代码块中

dbConn.Open() 
之前

dbConn.Close() 
您应该有如下代码:

SqlCommand sc = new SqlCommand("select prodid, proddesc from products", conn);
SqlDataReader reader;

reader = sc.ExecuteReader();
DataTable dt = new DataTable();
dt.Columns.Add("prodid", typeof(string));
dt.Columns.Add("proddesc", typeof(string));
dt.Load(reader);

cbxProducts.ValueMember = "prodid";
cbxProducts.DisplayMember = "proddesc";
cbxProducts.DataSource = dt;
然后在第二个代码块中,您需要首先获取所选的产品ID

private void cbxProducts_SelectedIndexChanged(object sender, EventArgs e)
{ 
    string ID = comboBox1.SelectedValue.ToString();

    //Based on ID make a sql query and fetch the details of that product from the DB. 
    //Follow the earlier code block to make DB connection and execute query.
    //Then populate the data in individual text boxes by looping through the dr.
    //I am leaving this deliberately for you to figure out and I am sure you can.

    txtProductNumber.Text = 
    txtProductDescription.Text = 
    txtProductCategory.Text = 
    txtProductCost.Text = 
}

在已更改的索引上,您需要打开另一个连接,并根据下拉列表中选定的值,制定SQL查询以从数据库中选择所需的字段,然后在文本框中填充这些字段。。。要做到这一点,您需要一个OleDbDataReader……您可以绑定到一个文本框,就像使用组合键一样,只需在每个文本框上设置源和成员。或者您可能缺少一些代码,从组合框的SelectedItem中获取ProductID,使用OleDbDataReader查询该代码,并在从事件处理程序调用的方法中简单地分配它们。