sql迭代两个父子表与关系

时间:2017-07-13 07:07:04

标签: sql sql-server common-table-expression

假设我有两个内部具有父子关系的表。

个人资料:

enter image description here

报告:

enter image description here

ProfileReport:

enter image description here

和UserProfile:

enter image description here

我的方案是首先找到用户的个人资料列表,然后找到用户个人资料的报告树。 我到目前为止尝试的是:

// Load excel data in to sql database
    private void BtnRefreshDatabase_Click(object sender, EventArgs e)
    {

    OpenFileDialog ope = new OpenFileDialog();
        //ope.Filter = "Excel File| *.xls ";
        if (ope.ShowDialog() == DialogResult.Cancel)
            return;
        FileStream stream = new FileStream(ope.FileName, FileMode.Open,FileAccess.Read);
        IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
        DataSet result = excelReader.AsDataSet();

        LinqToEntermedDatabaseDataContext conn = new LinqToEntermedDatabaseDataContext();
     // System.Data.Linq.Mapping.ColumnAttribute(storarge="Artikel",DbType="uniquIdentifier NOT NULL",IsPrimaryKey=true) volgens voorbeeld website stackoverflow
        foreach (DataTable table in result.Tables)
        {
            foreach (DataRow dr in table.Rows)
            {   
                TableOption addtable = new TableOption()
                {                        
                    Artikel = Convert.ToString(dr[0]),
                    Discription = Convert.ToString(dr[1]),
                    Extra1 = Convert.ToString(dr[2]),
                    Extra2 = Convert.ToString(dr[3]),
                    Kosts = Convert.ToDecimal(dr[4])

                };
                conn.TableOptions.InsertOnSubmit(addtable);

            }

            conn.SubmitChanges();
            excelReader.Close();
            stream.Close();

            MessageBox.Show("Database ok");
        }

    }

但我无法让报告树正确。有没有办法实现查找配置文件树,然后找到与配置文件树用户对应的报告树?

0 个答案:

没有答案