简体   繁体   English

如何从多个访问表中获取数据到C#Windows窗体中?

[英]How do i get data from multiple access tables into a C# windows form?

I am trying to display data from four different database tables using the code below, this method works perfectly for one table but i cant get my head around making it work for multiple, here is the code: 我正在尝试使用下面的代码显示来自四个不同数据库表的数据,该方法非常适合一个表,但是我无法理解使其适用于多个表,下面是代码:

private void Assign_Load(object sender, EventArgs e)
    {
        try
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = ConnectionDetail.Warehouse;
            string sql = "Select * from [Location], [Assign], [Products], [staff]";
            da = new OleDbDataAdapter(sql, conn);
            da.Fill(dt); // filling the database information into databtable

            //Location Table
            DataRow dr = dt.Rows[currentRow]; // counting the rows
            txtLocID.Text = dr["Location ID"].ToString();
            txtAisle.Text = dr["Aisle Code"].ToString(); 
            txtSection.Text = dr["Section"].ToString();
            txtShelf.Text = dr["Shelf"].ToString();
            txtLocation.Text = dr["Location"].ToString();
            txtLength.Text = dr["Length"].ToString();
            txtWidth.Text = dr["Width"].ToString();
            lstSize.Text = dr["Size Description"].ToString();

            //Product Table
            txtSKU.Text = dr["SKU"].ToString();
            txtDes.Text = dr["Description"].ToString(); 
            listHaz.Text = dr["Hazardous"].ToString();
            listLiq.Text = dr["Liquid"].ToString();
            txtQuan.Text = dr["Quantity"].ToString();
            txtWeig.Text = dr["Weight"].ToString();
            lstSize.Text = dr["Size Description"].ToString();

            //Satff Table
            txtID.Text = dr["StaffID"].ToString();

            //Assign Table
            txtAssID.Text = dr["Assign ID"].ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        {
            OleDbConnection load = new OleDbConnection();
            load.ConnectionString = ConnectionDetail.Warehouse;
            OleDbCommand cmds = new OleDbCommand();
            lbltime.Text = DateTime.Now.ToString("yyyy MMM ddd HH:mm");
            try
            {
                load.Open();
                cmds = new OleDbCommand();
                cmds.CommandText = "SELECT * FROM [Location], [Assign], [Products], [staff];";
                cmds.Connection = load;
                dr = cmds.ExecuteReader();   


            }
            catch (Exception err)
            {
                //Any database errors jump here and output error message
                MessageBox.Show("A database error has occurred: " + Environment.NewLine + err.Message);
            }
            finally
            {
               // btnNext_Click(sender, e);
                txtLocID.Visible = false;
            }
        }
    }

The best answer, is to update your SQL statement to use a JOIN statement. 最好的答案是将您的SQL语句更新为使用JOIN语句。 Assuming your data / tables are somehow related. 假设您的数据/表以某种方式相关。

Else, the other way to do it, is to create separate functions to populate each set of Text items separately. 另一种方法是创建单独的函数以分别填充每组Text项。

Looking at your code, I'm betting you should consider "binding". 查看您的代码,我敢打赌您应该考虑“绑定”。

Figured it out, this works just as well as all your suggestions, thanks for everyone's help anyway. 弄清楚了,这和您的所有建议一样有效,无论如何,感谢大家的帮助。

    OleDbConnection Cons = new OleDbConnection(); // new connection 
    OleDbDataReader dr;
    int currentRow = 0;

    //Location
    DataTable dt = new DataTable();
    OleDbDataAdapter da;

    //Product
    DataTable dt1 = new DataTable();
    OleDbDataAdapter da1;

    //staff
    DataTable dt2 = new DataTable();
    OleDbDataAdapter da2;

    //Assign
    DataTable dt3 = new DataTable();
    OleDbDataAdapter da3;

    private void Assign_Load(object sender, EventArgs e)
    {
        try
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = ConnectionDetail.Warehouse;
            string sql = "Select * from Location";
            da = new OleDbDataAdapter(sql, conn);
            da.Fill(dt); // filling the database information into databtable

            //Location Table
            DataRow dr = dt.Rows[currentRow]; // counting the rows
            txtLocID.Text = dr["Location ID"].ToString();
            txtAisle.Text = dr["Aisle Code"].ToString(); 
            txtSection.Text = dr["Section"].ToString();
            txtShelf.Text = dr["Shelf"].ToString();
            txtLocation.Text = dr["Location"].ToString();
            txtLength.Text = dr["Length"].ToString();
            txtWidth.Text = dr["Width"].ToString();
            lstSize.Text = dr["Size Description"].ToString();

            //Product Table
            OleDbConnection conn1 = new OleDbConnection();
            conn1.ConnectionString = ConnectionDetail.Warehouse;
            string sql1 = "Select * from Products";
            da1 = new OleDbDataAdapter(sql1, conn1);
            da1.Fill(dt1); // filling the database information into databtable

            DataRow dr1 = dt1.Rows[currentRow]; // counting the rows
            txtSKU.Text = dr1["SKU"].ToString();
            txtDes.Text = dr1["Description"].ToString(); 
            listHaz.Text = dr1["Hazardous"].ToString();
            listLiq.Text = dr1["Liquid"].ToString();
            txtQuan.Text = dr1["Quantity"].ToString();
            txtWeig.Text = dr1["Weight"].ToString();
            lstSize1.Text = dr1["Size Description"].ToString();

            //Staff Table
            OleDbConnection conn2 = new OleDbConnection();
            conn2.ConnectionString = ConnectionDetail.Warehouse;
            string sql2 = "Select * from Staff";
            da2 = new OleDbDataAdapter(sql2, conn2);
            da2.Fill(dt2); // filling the database information into databtable

            DataRow dr2 = dt2.Rows[currentRow]; // counting the rows
            txtID.Text = dr2["StaffID"].ToString();

            //Assign Table
            OleDbConnection conn3 = new OleDbConnection();
            conn3.ConnectionString = ConnectionDetail.Warehouse;
            string sql3 = "Select * from Assign";
            da3 = new OleDbDataAdapter(sql3, conn3);
            da3.Fill(dt3); // filling the database information into databtable

            DataRow dr3 = dt3.Rows[currentRow]; // counting the rows
            txtAssID.Text = dr3["Assign ID"].ToString();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        {
            OleDbConnection load = new OleDbConnection();
            load.ConnectionString = ConnectionDetail.Warehouse;
            OleDbCommand cmds = new OleDbCommand();
            lbltime.Text = DateTime.Now.ToString("yyyy MMM ddd HH:mm");
            try
            {
                load.Open();
                cmds = new OleDbCommand();
                cmds.CommandText = "SELECT * FROM Location;";
                cmds.CommandText = "SELECT * FROM Products;";
                cmds.CommandText = "SELECT * FROM Staff;";
                cmds.CommandText = "SELECT * FROM Assign;";
                cmds.Connection = load;
                dr = cmds.ExecuteReader();   


            }
            catch (Exception err)
            {
                //Any database errors jump here and output error message
                MessageBox.Show("A database error has occurred: " + Environment.NewLine + err.Message);
            }
            finally
            {
               // btnNext_Click(sender, e);
                txtLocID.Visible = false;
            }
        }
    }

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

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