与表格建立关系的问题 [英] Problem with make relations beetwen to tables

查看:101
本文介绍了与表格建立关系的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3张桌子:

I got 3 tables:

tb_plane          tb_attributes        tb_as_plane_attributes
plane_id          attribute_id         plane_attribute_id
plane_name        attribute_name       FK_plane_id
                                       FK_attribute_id
                                       value



我想要的是将属性与平面相关联,并为每个属性设置一个值.我确实做了一个组合,并在其中填充了所有plane_name's:



What I want to is to relate attributes to plane and set a value for each attribute. I did make a combo which i populate with all plane_name''s:

private void PopulatePlainComboBox()
        {
            try
            {
            string ConnectionString = conSettings.ConnectionString;

                DataSet dataSet = new DataSet();
                using (SqlConnection connection = new SqlConnection(ConnectionString))
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = connection;
                        using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
                        {
                            command.CommandText = ("SELECT * FROM tb_plane");
                            dataAdapter.Fill(dataSet, "tb_plane");

                            command.CommandText = ("SELECT plane_id, plane_name FROM tb_plane");
                            dataAdapter.Fill(dataSet, "tb_plane");

                            CboPlains.DataSource = dataSet.Tables["tb_plane"];
                            CboPlains.DisplayMember = "plane_name";
                            CboPlains.ValueMember = "plane_id";
                        }
                    }
                    connection.Close();
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string str;
                str = "Source:" + ex.Source;
                str += "\n" + "Message:" + ex.Message;
                MessageBox.Show(str, "Database Exception");
            }
            catch (System.Exception ex)
            {
                string str;
                str = "Source:" + ex.Source;
                str += "\n" + "Message:" + ex.Message;
                MessageBox.Show(str, "Generic Exception");
            }
            finally
            {
            }
        }



现在,我想用tb_attributes中的attribute_name的所有属性填充一个数据网格.对于所有attribute_name来说,第一个是column,对于Column-Value,则是第二个column.我想为要与飞机关联的属性设置值,并单击按钮进行关联并保存到表tb_as_plane_attributes中.我不放置值的其余属性未保存在关联表中.最后一次单击事件以接受.

我试图使这项工作,但我有问题.我尝试为此实现datagrid:



Now I want to populate a datagrid with all attributes by attribute_name''s from tb_attributes. One coulmn for all attribute_name''s and second for Column - Value. I want to set value to this attributes I want to relate with plane''s and by clicking button to make relations and save to table tb_as_plane_attributes. rest attributes where I don''t put values are not save in assocation table. At last button click event to accept.

I try to make this work but I have problems. I try to implement datagrid for that:

private void PopulateGridByAttributes()
        {
            try
            {
            string ConnectionString = conSettings.ConnectionString;

                DataSet dataSet = new DataSet();
                using (SqlConnection connection = new SqlConnection(ConnectionString))
                {
                    connection.Open();
                    using (SqlCommand command = new SqlCommand())
                    {
                        command.Connection = connection;
                        using (SqlDataAdapter dataAdapter = new SqlDataAdapter(command))
                        {
                            command.CommandText = ("SELECT attribute_id, attribute_name FROM tb_attributes");
                            dataAdapter.Fill(dataSet, "tb_attributes");

                            GrdAllAttributes.Columns.Add("attribute_name", "name of attribute");
                            GrdAllAttributes.Columns.Add("dd", "Value");
                            int row = dataSet.Tables["tb_attributes"].Rows.Count - 1;
                            for (int r = 0; r <= row; r++)
                            {
                                GrdAllAttributes.Rows.Add();
                                GrdAllAttributes.Rows[r].Cells[0].Value = dataSet.Tables["tb_Atributtes"].Rows[r].ItemArray[1];   //attribute_name
                            }
                        }
                    }
                    connection.Close();
                }
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                string str;
                str = "Source:" + ex.Source;
                str += "\n" + "Message:" + ex.Message;
                MessageBox.Show(str, "Database Exception");
            }
            catch (System.Exception ex)
            {
                string str;
                str = "Source:" + ex.Source;
                str += "\n" + "Message:" + ex.Message;
                MessageBox.Show(str, "Generic Exception");
            }
            finally
            {
            }
        }

推荐答案

我要说的是,您需要查看内部联接.尝试从多个表中获取数据时,您需要使用内部联接以确保检索到正确的数据.看一下此链接:

内部联接 [ ^ ]

希望这对您有帮助
I would say that you need to look at inner joins. When trying to get the data from multiple tables you need to use inner joins to ensure the correct data is retrieved. Take a look at this link:

Inner Joins[^]

Hope this helps


对于那些想要在我的项目中为您提供帮助的人:
项目

这是sql服务器名称"plane"的数据库脚本:
数据库

也许会有一些帮助..
For these who want to help here''s my project:
Project

And here''s a database script for sql server name "plane":
database

mayby some could help..


这篇关于与表格建立关系的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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