Пример #1
0
        public void SaveChangesToFile(string addressBookPath = @"C:\Program Files\InspectIT\Address.mdb")
        {
            if (File.Exists(addressBookPath) == false)
            {
                throw new FileNotFoundException();
            }

            using (OleDbConnection curOleConn = new OleDbConnection(string.Format("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = {0}", addressBookPath)))
                using (OleDbCommand curOleCommand = curOleConn.CreateCommand()) {
                    curOleConn.Open();

                    //Need to clear out all contacts which have been removed from the contact list in the template:

                    string pkValuesInContactList = getAllPkValuesFromContactsForUseWithSqlWhereInClause(this.AddressBookContactList);

                    if (pkValuesInContactList != "()")
                    {
                        curOleCommand.CommandText = "DELETE FROM `Address` WHERE [Address_ID] NOT IN " + pkValuesInContactList;
                        curOleCommand.ExecuteNonQuery();
                    }
                    else
                    {
                        curOleCommand.CommandText = "DELETE FROM `Address`";
                        curOleCommand.ExecuteNonQuery();
                    }

                    foreach (AddressBookContact curContact in this.AddressBookContactList)
                    {
                        if (curContact == null || string.IsNullOrWhiteSpace(curContact.Name) || curContact.ContactHasBeenModified == false)
                        {
                            continue;
                        }

                        AddressBookContact.CopyLogoToITpipesDirectory(curContact, "AddressBook");

                        if (curContact.SourceRecordIntPK > 0)
                        {
                            AddressBookContact.LoadCommandToUpdateExistingContactRecord(curOleCommand, curContact);
                        }

                        else
                        {
                            AddressBookContact.LoadCommandToInsertNewContactRecord(curOleCommand, curContact);
                        }

                        curOleCommand.ExecuteNonQuery();

                        if (curContact.SourceRecordIntPK > 0 == false)
                        {
                            curOleCommand.Parameters.Clear();
                            curOleCommand.CommandText    = "SELECT TOP 1 [Address_ID] FROM Address ORDER BY [Address_ID] DESC";
                            curContact.SourceRecordIntPK = (int)curOleCommand.ExecuteScalar();
                        }
                    }
                }
        }
Пример #2
0
        public void SaveTemplate(bool copyContactLogoToITpipesDirectory = true)
        {
            if (string.IsNullOrEmpty(this.PathToTemplateFile) ||
                string.IsNullOrEmpty(this._pathToTemporarilyExtractedProjectMdb))
            {
                throw new Exception("Cannot save a template whose PathToTemplateFile or _pathToTemporarilyExtractedProjectMdb is null or empty");
            }

            if (File.Exists(this.PathToTemplateFile) == false ||
                File.Exists(this._pathToTemporarilyExtractedProjectMdb) == false)
            {
                throw new FileNotFoundException(string.Format("Template file or temporary file not found:\nTemplate: {0}\nTemp File: {1}", this.PathToTemplateFile, this._pathToTemporarilyExtractedProjectMdb));
            }

            using (OleDbConnection curOleConn = new OleDbConnection(this._privateOleConnString))
                using (OleDbCommand curOleCommand = curOleConn.CreateCommand()) {
                    curOleConn.Open();

                    //Need to clear out all contacts which have been removed from the contact list in the template:

                    string pkValuesInContactList = getAllPkValuesFromContactsForUseWithSqlWhereInClause(this.TemplateContacts);

                    if (pkValuesInContactList != "()")
                    {
                        curOleCommand.CommandText = "DELETE FROM `Info` WHERE [Info_ID] NOT IN " + pkValuesInContactList;
                        curOleCommand.ExecuteNonQuery();
                    }
                    else
                    {
                        curOleCommand.CommandText = "DELETE FROM `Info`";
                        curOleCommand.ExecuteNonQuery();
                    }

                    foreach (AddressBookContact curContact in this.TemplateContacts)
                    {
                        if (curContact == null || string.IsNullOrWhiteSpace(curContact.Name) || curContact.ContactHasBeenModified == false)
                        {
                            continue;
                        }

                        AddressBookContact.CopyLogoToITpipesDirectory(curContact, this.TemplateName);

                        if (curContact.SourceRecordIntPK > 0)
                        {
                            AddressBookContact.LoadCommandToUpdateExistingContactRecord(curOleCommand, curContact);
                        }

                        else
                        {
                            AddressBookContact.LoadCommandToInsertNewContactRecord(curOleCommand, curContact);
                        }

                        curOleCommand.ExecuteNonQuery();

                        if (curContact.SourceRecordIntPK < 1)
                        {
                            curOleCommand.Parameters.Clear();
                            curOleCommand.CommandText    = "SELECT TOP 1 [Info_ID] FROM Info ORDER BY [Info_ID] DESC";
                            curContact.SourceRecordIntPK = (int)curOleCommand.ExecuteScalar();
                        }
                    }
                }

            string pathToTempMdb = Path.Combine(Path.GetTempPath(), "project.mdb");

            if (File.Exists(pathToTempMdb))
            {
                File.Delete(pathToTempMdb);
            }

            File.Copy(this._pathToTemporarilyExtractedProjectMdb, pathToTempMdb);

            Ionic.Zip.ZipEntry projectMdbEntry = null;

            foreach (var curEntry in TemplateZipObject.Entries)
            {
                if (curEntry.FileName == "project.mdb")
                {
                    projectMdbEntry = curEntry;
                }
            }

            if (projectMdbEntry != null)
            {
                TemplateZipObject.RemoveEntry(projectMdbEntry);
            }

            projectMdbEntry = null;


            TemplateZipObject.Encryption = Ionic.Zip.EncryptionAlgorithm.PkzipWeak;
            TemplateZipObject.AddFile(pathToTempMdb, "\\").Password = "******";

            string tempFolderPath = Path.GetTempPath() + Path.GetFileNameWithoutExtension(TemplateZipObject.Name);

            if (Directory.Exists(tempFolderPath) == false)
            {
                Directory.CreateDirectory(tempFolderPath);
            }

            TemplateZipObject.Save();
            TemplateZipObject.Reset(false); //If you don't reset the zip object, project.mdb becomes corrupt after the second consecutive save--not sure why.
        }