コード例 #1
0
        /// <summary>Backs up the database, using the specified database connection as the destination.</summary>
        /// <param name="destination">The destination database connection.</param>
        /// <param name="destinationName">The destination database name (usually <c>"main"</c>).</param>
        /// <param name="sourceName">The source database name (usually <c>"main"</c>).</param>
        /// <param name="pages">The number of pages to copy, or negative to copy all remaining pages.</param>
        /// <param name="callback">The method to invoke between each step of the backup process.  This
        /// parameter may be <c>null</c> (i.e., no callbacks will be performed).</param>
        /// <param name="retryMilliseconds">The number of milliseconds to sleep after encountering a locking error
        /// during the backup process.  A value less than zero means that no sleep should be performed.</param>
        public void BackupDatabase(SQLiteConnection destination, string destinationName, string sourceName, int pages, SQLiteBackupCallback callback, int retryMilliseconds)
        {
            VerifyNotDisposed();
            if (m_connectionState != ConnectionState.Open)
            {
                throw new InvalidOperationException("Source database is not open.");
            }
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }
            if (destination.m_connectionState != ConnectionState.Open)
            {
                throw new ArgumentException("Destination database is not open.", "destination");
            }
            if (destinationName == null)
            {
                throw new ArgumentNullException("destinationName");
            }
            if (sourceName == null)
            {
                throw new ArgumentNullException("sourceName");
            }
            if (pages == 0)
            {
                throw new ArgumentException("pages must not be 0.", "pages");
            }

            using (SqliteBackupHandle backup = NativeMethods.sqlite3_backup_init(destination.m_db, ToNullTerminatedUtf8(destinationName), m_db, ToNullTerminatedUtf8(sourceName)))
            {
                if (backup == null)
                {
                    throw new SQLiteException(NativeMethods.sqlite3_errcode(m_db), m_db);
                }

                while (true)
                {
                    SQLiteErrorCode error = NativeMethods.sqlite3_backup_step(backup, pages);

                    if (error == SQLiteErrorCode.Done)
                    {
                        break;
                    }
                    else if (error == SQLiteErrorCode.Ok || error == SQLiteErrorCode.Busy || error == SQLiteErrorCode.Locked)
                    {
                        bool retry = error != SQLiteErrorCode.Ok;
                        if (callback != null && !callback(this, sourceName, destination, destinationName, pages, NativeMethods.sqlite3_backup_remaining(backup), NativeMethods.sqlite3_backup_pagecount(backup), retry))
                        {
                            break;
                        }

                        if (retry && retryMilliseconds > 0)
                        {
                            Thread.Sleep(retryMilliseconds);
                        }
                    }
                    else
                    {
                        throw new SQLiteException(error, m_db);
                    }
                }
            }
        }
コード例 #2
0
 public static extern int sqlite3_backup_remaining(SqliteBackupHandle p);
コード例 #3
0
 public static extern int sqlite3_backup_pagecount(SqliteBackupHandle p);
コード例 #4
0
 public static extern SQLiteErrorCode sqlite3_backup_step(SqliteBackupHandle p, int nPage);