Exemplo n.º 1
0
        private void SetLinks()
        {
            _Links = new List <DataLink>();

            foreach (DataField field in Dummy.Fields)
            {
                if (field.ChildTableTypes == null)
                {
                    continue;
                }
                foreach (Type childType in field.ChildTableTypes)
                {
                    {
                        DataRecord childDummy = SQLDMGlobal.FindDummy(childType);
                        if (childDummy == null)
                        {
                            continue;
                        }

                        foreach (DataField childField in childDummy.Dummy.Fields)
                        {
                            if (childField.ParentTableType != null && childField.ParentTableType.FullName == GetType().FullName)
                            {
                                DataLink newLink = new DataLink(this, field, childType, childField.SourceColumn);
                                _Links.Add(newLink);
                                break;
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        protected void LoadChildRecords(Type type)
        {
            using (SqlConnection conn = new SqlConnection(SQLDMGlobal.ConnectionString()))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();

                try
                {
                    foreach (DataLink dl in Links.FindAll(c => c.ChildRecordType == type))
                    {
                        DataRecord childDummy = SQLDMGlobal.FindDummy(type);

                        if (childDummy != null)
                        {
                            cmd.CommandText = string.Format("select * from {0} where [{1}] = {2}", childDummy.FullSafeTableName, dl.ChildFieldName, dl.ParentField.ParameterName);
                            SqlParameter param = GetSqlParameter(dl.ParentField.ParameterName);
                            if (param != null)
                            {
                                cmd.Parameters.Add(param);
                            }

                            SqlDataAdapter da = new SqlDataAdapter();
                            da.SelectCommand = cmd;
                            DataTable dt = new DataTable();
                            da.Fill(dt);

                            foreach (DataRow row in dt.Rows)
                            {
                                DataRecord inst = (DataRecord)System.Activator.CreateInstance(dl.ChildRecordType);
                                FillRecord(row, inst, true);
                                ChildRecords.Add(inst);
                            }

                            cmd.Parameters.Clear();
                        }
                    }
                }
                catch (Exception ex)
                {
                    if (conn.State == ConnectionState.Open)
                    {
                        conn.Close();
                    }
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
        }