private void copyRelatedRecords(Cinar.Database.Database dbSrc, Table tblSrc, int idSrc, Cinar.Database.Database dbDst, Table tblDst, int idDst) { foreach (Table tbl in tblSrc.ReferencedByTables) { Column fld = tbl.FindColumnWhichRefersTo(tblSrc); string sql = string.Format("select * from [{0}] where [{1}] = {{0}}", tbl.Name, fld.Name); DataTable dt = dbSrc.GetDataTable(sql, idSrc); if (dt == null || dt.Rows.Count == 0 || dbDst.Tables[tbl.Name] == null) continue; foreach (DataRow dr in dt.Rows) { Hashtable ht = dbSrc.DataRowToHashtable(dr); ht.Remove(tbl.PrimaryColumn.Name); ht[tbl.PrimaryColumn.Name] = 0; ht.Remove(fld.Name); ht[fld.Name] = idDst; dbDst.Insert(tbl.Name, ht); } backgroundWorker.ReportProgress(100, Environment.NewLine + string.Format("{0} records copied from {1}.", dt.Rows.Count, tbl.Name)); } }
private void copyRecords(Cinar.Database.Database dbSrc, Table tblSrc, Column fldSrc, int parentIdSrc, Cinar.Database.Database dbDst, Table tblDst, Column fldDst, int parentIdDst) { string sql = string.Format("select * from [{0}] where [{1}] = {{0}}", tblSrc.Name, fldSrc.Name); DataTable dt = dbSrc.GetDataTable(sql, parentIdSrc); if (dt == null || dt.Rows.Count == 0) return; foreach (DataRow dr in dt.Rows) { Hashtable ht = dbSrc.DataRowToHashtable(dr); ht.Remove(tblSrc.PrimaryColumn.Name); ht[tblDst.PrimaryColumn.Name] = 0; ht.Remove(fldSrc.Name); ht[fldDst.Name] = parentIdDst; dbDst.Insert(tblDst.Name, ht); backgroundWorker.ReportProgress(0, "."); int newId = dbDst.GetInt("select max(" + tblDst.PrimaryColumn.Name + ") from [" + tblDst.Name + "]"); copyRecords(dbSrc, tblSrc, fldSrc, (int)dr[tblSrc.PrimaryColumn.Name], dbDst, tblDst, fldDst, newId); copyRelatedRecords(dbSrc, tblSrc, (int)dr[tblSrc.PrimaryColumn.Name], dbDst, tblDst, newId); } }