示例#1
0
        public int ImportFromNewDB(string conString, bool importMarks)
        {
            //Open the other DB
            if (conString == ConnectionString || !conString.EndsWith("s3db"))
                throw new InvalidDataException("Invalid Database");

            int affectedRows = 0;
            LanguageData otherDBLayer = new LanguageData(conString);
            Dictionary<int, int> cardIDHashTable = new Dictionary<int, int>();

            int totalRows = int.Parse(otherDBLayer.daCard.Count().ToString());
            totalRows += int.Parse(otherDBLayer.daCardList.Count().ToString());
            totalRows += int.Parse(otherDBLayer.daCollection.Count().ToString());
            totalRows += int.Parse(otherDBLayer.daDictionary.Count().ToString());

            int progress = 0;

            //Import the Cards
            foreach (dsLanguageData.CardRow otherCard in otherDBLayer.daCard.GetData())
            {
                progress++;

                //Check for dups
                dsLanguageData.CardDataTable dtCard = daCard.GetDataByCardInfo(otherCard.Question, otherCard.Answer);
                if (dtCard.Rows.Count > 0)
                {
                    //Don't insert it, just add the ID to the Dictionary
                    cardIDHashTable.Add(otherCard.ID, dtCard[0].ID);
                    DataImported(new DataImportedEventArgs(true, string.Format("Skipped Card: {0}", otherCard.Answer), progress, totalRows));
                }
                else
                {
                    //insert the card and add the ID to the Dictionary
                    dsLanguageData.CardRow tmpCard = dsLanguageData.CardDataTable.CloneRow(otherCard, new dsLanguageData.CardDataTable());
                    tmpCard.ID = -1;

                    if (!importMarks)
                    {
                        tmpCard.ClearMarks();
                    }

                    affectedRows += InsertOrUpdateCard(tmpCard);
                    cardIDHashTable.Add(otherCard.ID, tmpCard.ID);
                    DataImported(new DataImportedEventArgs(true, string.Format("Added Card: {0}", tmpCard.Answer), progress, totalRows));

                    //insert the soundfile
                    if (otherDBLayer.CardHasSound(otherCard.ID))
                    {
                        dsLanguageData.SoundClipRow otherSoundRow = otherDBLayer.daSoundClip.GetDataByCardID(otherCard.ID)[0];
                        dsLanguageData.SoundClipRow tmpSound = dsLanguageData.SoundClipDataTable.CloneRow(otherSoundRow);
                        tmpSound.ID = -1;

                        affectedRows += InsertUpdateOrDeleteSoundClip(tmpSound);
                    }

                    //insert the picture
                    if (otherDBLayer.CardHasPicture(otherCard.ID))
                    {
                        dsLanguageData.PictureRow otherPicRow = otherDBLayer.daPicture.GetDataByCardID(otherCard.ID)[0];
                        affectedRows += InsertUpdatePicture(ByteArrayToImage(otherPicRow.Image), tmpCard.ID, otherPicRow.SearchText);
                    }
                }
            }//end foreach

            //Collection
            Dictionary<int, int> collectionHashTable = new Dictionary<int, int>();
            foreach (dsLanguageData.CollectionRow otherCollection in otherDBLayer.daCollection.GetData())
            {
                progress++;

                //check for dups
                if (daCollection.GetDataByName(otherCollection.Name).Rows.Count > 0)
                {
                    collectionHashTable.Add(otherCollection.ID, daCollection.GetDataByName(otherCollection.Name)[0].ID);
                    DataImported(new DataImportedEventArgs(true, string.Format("Skipped Collection: {0}", otherCollection.Name), progress, totalRows));
                }
                else
                {
                    //Insert the new collection
                    dsLanguageData.CollectionRow tmpCollection = dsLanguageData.CollectionDataTable.CloneRow(otherCollection);
                    tmpCollection.ID = -1;

                    affectedRows += InsertCollection(tmpCollection);
                    collectionHashTable.Add(otherCollection.ID, tmpCollection.ID);
                    DataImported(new DataImportedEventArgs(true, string.Format("Added Collection: {0}", tmpCollection.Name), progress, totalRows));
                }
            }

            //CardList
            Dictionary<int, int> cardListHashTable = new Dictionary<int, int>();
            foreach (dsLanguageData.CardListRow otherCardList in otherDBLayer.daCardList.GetData())
            {
                progress++;

                //Check for dups
                if (daCardList.GetDataByName(otherCardList.Name).Rows.Count > 0)
                {
                    cardListHashTable.Add(otherCardList.ID, daCardList.GetDataByName(otherCardList.Name)[0].ID);
                    DataImported(new DataImportedEventArgs(true, string.Format("Skipped CardList: {0}", otherCardList.Name), progress, totalRows));
                }
                else
                {
                    //insert the new list
                    dsLanguageData.CardListRow tmpCardList = dsLanguageData.CardListDataTable.CloneRow(otherCardList);
                    tmpCardList.ID = -1;
                    tmpCardList.CollectionID = collectionHashTable[otherCardList.CollectionID];

                    affectedRows += InsertCardList(tmpCardList);
                    cardListHashTable.Add(otherCardList.ID, tmpCardList.ID);
                    DataImported(new DataImportedEventArgs(true, string.Format("Added CardList: {0}", tmpCardList.Name), progress, totalRows));
                }

                //CardListData
                foreach (dsLanguageData.CardListDataRow otherCardListData in otherDBLayer.daCardListData.GetDataByCardListID(otherCardList.ID))
                {
                    //If there if it is not already there
                    if (daCardListData.GetDataByIDs(cardListHashTable[otherCardList.ID], cardIDHashTable[otherCardListData.CardID]).Rows.Count < 1)
                    {
                        //add the cardID to the list
                        affectedRows += InsertCardListDataItem(cardListHashTable[otherCardList.ID], cardIDHashTable[otherCardListData.CardID]);
                    }
                }//end foreach

            }//end foreach

            //Dictionry
            foreach (dsLanguageData.DictionaryRow otherDictionary in otherDBLayer.daDictionary.GetData())
            {
                progress++;

                //if it is not a duplicate
                if (daDictionary.GetDataByName(otherDictionary.Name).Rows.Count < 1)
                {
                    //insert it
                    dsLanguageData.DictionaryRow tmpDictionary = dsLanguageData.DictionaryDataTable.CloneRow(otherDictionary);
                    tmpDictionary.ID = -1;

                    affectedRows += InsertOrUpdateDictionary(tmpDictionary);
                    DataImported(new DataImportedEventArgs(true, string.Format("Added Dictionary: {0}", tmpDictionary.Name), progress, totalRows));
                }
                else
                {
                    DataImported(new DataImportedEventArgs(true, string.Format("Skipped Dictionary: {0}", otherDictionary.Name), progress, totalRows));
                }
            }

            return affectedRows;
        }