コード例 #1
0
        private static void CreateMdfFile(string fullFileName, string dataDir, string connectionString)
        {
            bool        flag       = false;
            string      database   = null;
            HttpContext current    = HttpContext.Current;
            string      dbFileName = null;

            try
            {
                if (!Directory.Exists(dataDir))
                {
                    flag = true;
                    Directory.CreateDirectory(dataDir);
                    flag = false;
                    try
                    {
                        if (current != null)
                        {
                            HttpRuntime.RestrictIISFolders(current);
                        }
                    }
                    catch
                    {
                    }
                }
                fullFileName = fullFileName.ToUpper(CultureInfo.InvariantCulture);
                char[] chArray = Path.GetFileNameWithoutExtension(fullFileName).ToCharArray();
                for (int i = 0; i < chArray.Length; i++)
                {
                    if (!char.IsLetterOrDigit(chArray[i]))
                    {
                        chArray[i] = '_';
                    }
                }
                string str3 = new string(chArray);
                if (str3.Length > 30)
                {
                    database = str3.Substring(0, 30) + "_" + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
                }
                else
                {
                    database = str3 + "_" + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
                }
                dbFileName = Path.Combine(Path.GetDirectoryName(fullFileName), str3 + "_TMP.MDF");
                SqlServices.Install(database, dbFileName, connectionString);
                DetachDB(database, connectionString);
                try
                {
                    File.Move(dbFileName, fullFileName);
                }
                catch
                {
                    if (!File.Exists(fullFileName))
                    {
                        File.Copy(dbFileName, fullFileName);
                        try
                        {
                            File.Delete(dbFileName);
                        }
                        catch
                        {
                        }
                    }
                }
                try
                {
                    File.Delete(dbFileName.Replace("_TMP.MDF", "_TMP_log.LDF"));
                }
                catch
                {
                }
            }
            catch (Exception exception)
            {
                if ((current == null) || current.IsCustomErrorEnabled)
                {
                    throw;
                }
                HttpException exception2 = new HttpException(exception.Message, exception);
                if (exception is UnauthorizedAccessException)
                {
                    exception2.SetFormatter(new SqlExpressConnectionErrorFormatter(flag ? DataConnectionErrorEnum.CanNotCreateDataDir : DataConnectionErrorEnum.CanNotWriteToDataDir));
                }
                else
                {
                    exception2.SetFormatter(new SqlExpressDBFileAutoCreationErrorFormatter(exception));
                }
                throw exception2;
            }
        }
コード例 #2
0
        private static void CreateMdfFile(string fullFileName, string dataDir, string connectionString)
        {
            bool        creatingDir  = false;
            string      databaseName = null;
            HttpContext context      = HttpContext.Current;
            string      tempFileName = null;

            try {
                if (!Directory.Exists(dataDir))
                {
                    creatingDir = true;
                    Directory.CreateDirectory(dataDir);
                    creatingDir = false;
                    try {
                        if (context != null)
                        {
                            HttpRuntime.RestrictIISFolders(context);
                        }
                    }
                    catch { }
                }

                fullFileName = fullFileName.ToUpper(CultureInfo.InvariantCulture);
                char[] strippedFileNameChars = Path.GetFileNameWithoutExtension(fullFileName).ToCharArray();
                for (int iter = 0; iter < strippedFileNameChars.Length; iter++)
                {
                    if (!char.IsLetterOrDigit(strippedFileNameChars[iter]))
                    {
                        strippedFileNameChars[iter] = '_';
                    }
                }
                string strippedFileName = new string(strippedFileNameChars);
                if (strippedFileName.Length > 30)
                {
                    databaseName = strippedFileName.Substring(0, 30) + "_" + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
                }
                else
                {
                    databaseName = strippedFileName + "_" + Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
                }

                tempFileName = Path.Combine(Path.GetDirectoryName(fullFileName), strippedFileName + "_TMP" + s_strSqlExprFileExt);

                // Auto create the temporary database
                SqlServices.Install(databaseName, tempFileName, connectionString);
                DetachDB(databaseName, connectionString);
                try {
                    File.Move(tempFileName, fullFileName);
                }
                catch {
                    if (!File.Exists(fullFileName))
                    {
                        File.Copy(tempFileName, fullFileName);
                        try {
                            File.Delete(tempFileName);
                        }
                        catch { }
                    }
                }
                try {
                    File.Delete(tempFileName.Replace("_TMP.MDF", "_TMP_log.LDF"));
                }
                catch { }
            }
            catch (Exception e) {
                if (context == null || context.IsCustomErrorEnabled)
                {
                    throw;
                }
                HttpException httpExec = new HttpException(e.Message, e);
                if (e is UnauthorizedAccessException)
                {
                    httpExec.SetFormatter(new SqlExpressConnectionErrorFormatter(creatingDir ? DataConnectionErrorEnum.CanNotCreateDataDir : DataConnectionErrorEnum.CanNotWriteToDataDir));
                }
                else
                {
                    httpExec.SetFormatter(new SqlExpressDBFileAutoCreationErrorFormatter(e));
                }
                throw httpExec;
            }
        }