Exemple #1
0
        ///<summary>Creates an empty database file.</summary>
        ///<param name="filePath">The path to the file.  If the file already exists, it will be overwritten.</param>
        ///<param name="format">The format of the file.</param>
        ///<returns>A DBConnector that connects to the path.</returns>
        public static DBConnector CreateFile(string filePath, DatabaseFile format)
        {
            switch (format)
            {
            case DatabaseFile.Access:
            case DatabaseFile.Access2007:
                //OleDB can't create Access databases, so I
                //embedded empty databases in the assembly.
                //I append a dummy extension so Web Publish
                //doesn't break.
                //https://connect.microsoft.com/VisualStudio/feedback/details/808438/loading-an-access-accdb-database-breaks-ftp-web-publish
                using (var originalStream = typeof(DB).Assembly.GetManifestResourceStream("ShomreiTorah.Common.Data." + format.ToString() + format.GetExtension() + ".template"))
                    using (var file = File.Create(filePath)) {
                        originalStream.CopyTo(file);
                    }
                break;

            case DatabaseFile.Excel:
            case DatabaseFile.Excel2007:
            case DatabaseFile.Excel2007Binary:
            case DatabaseFile.Excel2007Macro:
                //It is not possible to have an empty Excel
                //file, so I just delete any existing file.
                //The file will be automatically created if
                //the client creates a table.
                File.Delete(filePath);
                break;

            case DatabaseFile.SqlCe:
                using (var engine = new SqlCeEngine(CreateSqlCeConnectionString(filePath)))
                    engine.CreateDatabase();
                break;

            default:
                break;
            }

            return(DB.OpenFile(filePath, format));
        }