⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sqlservices.cs

📁 MasterPage(母版页) 母版页(MasterPage)就相当于模板页
💻 CS
📖 第 1 页 / 共 2 页
字号:
//------------------------------------------------------------------------------
// <copyright file="Configuration.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

namespace Microsoft.Samples {
    using System.Web.Management;
    using System.Web;
    using System;
    using System.Web.Util;
    using System.IO;
    using System.Data;
    using System.Data.SqlClient;
    using System.Threading;
    using System.Text.RegularExpressions;
    using System.Text;
    using System.Security;
    using System.Security.Permissions;
    using System.Globalization;
    using System.Runtime.Serialization;
    using System.Collections;

    // Remove CAS from sample: [AspNetHostingPermission(SecurityAction.LinkDemand, Level=AspNetHostingPermissionLevel.High)]
    // Remove CAS from sample: [AspNetHostingPermission(SecurityAction.InheritanceDemand, Level=AspNetHostingPermissionLevel.High)]
    public static class MicrosoftSamples_SqlServices {
        public static void Install(string server, string user, string password, string database, SqlFeatures features) {
            SetupApplicationServices(server, user, password, false, null, database, null, features, true);
        }

        // For trusted connection
        public static void Install(string server, string database, SqlFeatures features) {
            SetupApplicationServices(server, null, null, true, null, database, null, features, true);
        }

        internal static void Install( string database, string dbFileName, string connectionString ) {
            SetupApplicationServices( null, null, null, false, connectionString, database, dbFileName, SqlFeatures.All, true );
        }

        // For connection using connection string
        public static void Install(string database, SqlFeatures features, string connectionString) {
            SetupApplicationServices(null, null, null, true, connectionString, database, null, features, true);
        }

        public static void Uninstall(string server, string user, string password, string database, SqlFeatures features) {
            SetupApplicationServices(server, user, password, false, null, database, null, features, false);
        }

        // For trusted connection
        public static void Uninstall(string server, string database, SqlFeatures features) {
            SetupApplicationServices(server, null, null, true, null, database, null, features, false);
        }

        // For connection using connection string
        public static void Uninstall(string database, SqlFeatures features, string connectionString) {
            SetupApplicationServices(null, null, null, true, connectionString, database, null, features, false);
        }

        public static void InstallSessionState(string server, string user, string password, string customDatabase, SessionStateType type) {
            SetupSessionState(server, user, password, false, null, customDatabase, type, true);
        }

        // For trusted connection
        public static void InstallSessionState(string server, string customDatabase, SessionStateType type) {
            SetupSessionState(server, null, null, true, null, customDatabase, type, true);
        }

        // For connection using connection string
        public static void InstallSessionState(string customDatabase, SessionStateType type, string connectionString) {
            SetupSessionState(null, null, null, true, connectionString, customDatabase, type, true);
        }


        public static void UninstallSessionState(string server, string user, string password, string customDatabase, SessionStateType type) {
            SetupSessionState(server, user, password, false, null, customDatabase, type, false);
        }

        // For trusted connection
        public static void UninstallSessionState(string server, string customDatabase, SessionStateType type) {
            SetupSessionState(server, null, null, true, null, customDatabase, type, false);
        }

        // For connection using connection string
        public static void UninstallSessionState(string customDatabase, SessionStateType type, string connectionString) {
            SetupSessionState(null, null, null, true, connectionString, customDatabase, type, false);
        }

        // Used by suite
        internal static ArrayList ApplicationServiceTables {
            get {
                ArrayList   tables = new ArrayList();
                for (int i=0; i <  s_featureInfos.Length; i++) {
                    tables.InsertRange(tables.Count, s_featureInfos[i]._tablesRemovedInUninstall);
                }
                return tables;
            }
        }

        public static string GenerateSessionStateScripts(bool install, SessionStateType type, string customDatabase) {
            SessionStateParamCheck(type, ref customDatabase);
            string  fullpath = Path.Combine(System.Web.HttpRuntime.AspInstallDirectory, install ? SESSION_STATE_INSTALL_FILE : SESSION_STATE_UNINSTALL_FILE);
            string  content  = File.ReadAllText(fullpath);
            return FixContent(content, customDatabase, null, true, type);
        }


        // Information about a feature.
        internal struct FeatureInfo {
            internal SqlFeatures    _feature;
            internal string[]       _installFiles;      // SQL file to run to install the feature
            internal string[]       _uninstallFiles;    // SQL file to run to uninstall the feature
            internal string[]       _tablesRemovedInUninstall;     // SQL tables to be removed during uninstall
            internal int            _dataCheckBitMask;

            internal FeatureInfo(SqlFeatures feature, string[] installFiles,
                string[] uninstallFiles, string[] tablesRemovedInUninstall, int dataCheckBitMask) {
                _feature = feature;
                _installFiles = installFiles;
                _uninstallFiles = uninstallFiles;
                _tablesRemovedInUninstall = tablesRemovedInUninstall;
                _dataCheckBitMask = dataCheckBitMask;
            }
        };

        static string INSTALL_COMMON_SQL = "InstallCommon.sql";

        static FeatureInfo[] s_featureInfos = {
            new FeatureInfo(SqlFeatures.Membership,
                            new string[] {INSTALL_COMMON_SQL, "InstallMembership.sql"},
                            new string[] {"UninstallMembership.sql"},
                            new string[] {"aspnet_Membership"},
                            1),

            new FeatureInfo(SqlFeatures.Profile,
                            new string[] {INSTALL_COMMON_SQL, "InstallProfile.sql"},
                            new string[] {"UninstallProfile.sql"},
                            new string[] {"aspnet_Profile"},
                            4),

            new FeatureInfo(SqlFeatures.RoleManager,
                            new string[] {INSTALL_COMMON_SQL, "InstallRoles.sql"},
                            new string[] {"UninstallRoles.sql"},
                            new string[] {"aspnet_Roles", "aspnet_UsersInRoles"},
                            2),

            new FeatureInfo(SqlFeatures.Personalization,
                            new string[] {INSTALL_COMMON_SQL, "InstallPersonalization.sql"},
                            new string[] {"UninstallPersonalization.sql"},
                            new string[] {"aspnet_PersonalizationPerUser", "aspnet_Paths", "aspnet_PersonalizationAllUsers"},
                            8),

            new FeatureInfo(SqlFeatures.SqlWebEventProvider,
                            new string[] {INSTALL_COMMON_SQL, "InstallWebEventSqlProvider.sql"},
                            new string[] {"UninstallWebEventSqlProvider.sql"},
                            new string[] {"aspnet_WebEvent_Events"},
                            16),

            // The following are the files to run in addition to those listed in other feature
            new FeatureInfo(SqlFeatures.All,
                            new string[] {},
                            new string[] {"UninstallCommon.sql"},
                            new string[] {"aspnet_Applications", "aspnet_Users", "aspnet_SchemaVersions"},
                            0x7FFFFFFF),
        };

        // Default database to use if the database name isn't supplied by the user
        static string   DEFAULT_DB = "aspnetdb";
        static string   ASPSTATE_DB = "ASPState";
        static string   SSTYPE_PERSISTED = "sstype_persisted";
        static string   SSTYPE_CUSTOM = "sstype_custom";

        static string   SESSION_STATE_INSTALL_FILE = "InstallSqlState.sql";
        static string   SESSION_STATE_UNINSTALL_FILE = "UninstallSqlState.sql";

        // Return a list of files based on install (bool) and features
        static ArrayList GetFiles(bool install, SqlFeatures features) {
            ArrayList   results = new ArrayList();
            bool        installCommonProcessed = false;

            // Load and modify the sql file for each feature
            for (int i=0; i <  s_featureInfos.Length; i++) {
                string[] sqlFiles = null;

                if (((int)s_featureInfos[i]._feature & (int)features) == (int)s_featureInfos[i]._feature) {
                    // We found one feature
                    if (install) {
                        sqlFiles = s_featureInfos[i]._installFiles;
                    }
                    else {
                        sqlFiles = s_featureInfos[i]._uninstallFiles;
                    }
                }

                if (sqlFiles != null) {
                    for(int j = 0; j < sqlFiles.Length; j++) {
                        string sqlFile = sqlFiles[j];
                        if (sqlFile != null) {
                            // We only need to process InstallCommon.sql once
                            if (sqlFile == INSTALL_COMMON_SQL && installCommonProcessed) {
                                continue;
                            }

                            results.Add(sqlFile);

                            if (!installCommonProcessed && sqlFile == INSTALL_COMMON_SQL) {
                                installCommonProcessed = true;
                            }
                        }
                    }
                }
            }

            return results;
        }

        // Replace the name of the database with the one specified by the caller
        static string FixContent(
            string content,
            string database,
            string dbFileName,
            bool sessionState,
            SessionStateType sessionStatetype
            )
        {
            if (database != null) {
                database = RemoveSquareBrackets(database);
            }

            if (sessionState) {
                if (sessionStatetype == SessionStateType.Temporary) {
                    // No change
                }
                else if (sessionStatetype == SessionStateType.Persisted) {
                    content = content.Replace("'sstype_temp'", "'" + SSTYPE_PERSISTED + "'");
                    content = content.Replace("[tempdb]", "[" + ASPSTATE_DB + "]");
                }
                else if (sessionStatetype == SessionStateType.Custom) {
                    content = content.Replace("'sstype_temp'", "'" + SSTYPE_CUSTOM + "'");
                    content = content.Replace("[tempdb]", "[" + database + "]");
                    content = content.Replace("'ASPState'", "'" + database + "'");
                    content = content.Replace("[ASPState]", "[" + database + "]");
                }
            }
            else {
                content = content.Replace("'aspnetdb'", "'" + database.Replace("'", "''") + "'");
                content = content.Replace("[aspnetdb]", "[" + database + "]");
            }

            if( dbFileName != null )
            {
                if (dbFileName.Contains("[") || dbFileName.Contains("]") || dbFileName.Contains("'"))
                    throw new ArgumentException(SR.GetString(SR.DbFileName_can_not_contain_invalid_chars));
                database = database.TrimStart( '[' );
                database = database.TrimEnd( ']' );

                string logicalFileName = database + "_DAT";
                if (!char.IsLetter(logicalFileName[0]))
                    logicalFileName = "A" + logicalFileName;

                //
                // Build the database options string for SQL Express database
                //
                string dbOptions = "ON ( NAME = " + logicalFileName + ", FILENAME = ''" +
                                    dbFileName + "'', " + "SIZE = 10MB, FILEGROWTH = 5MB )";

                content = content.Replace("SET @dboptions = N'/**/'",
                                            "SET @dboptions = N'" + dbOptions + "'");
            }

            return content;
        }

        static void ExecuteSessionFile(
            string file,
            string server,
            string database,
            string dbFileName,
            SqlConnection connection,
            bool isInstall,
            SessionStateType sessionStatetype
            ) {
            ExecuteFile(file, server, database, dbFileName, connection, true, isInstall, sessionStatetype);
        }

        // Load the SQL file, change the database name within, and execute it.
        static void ExecuteFile(
            string file,
            string server,
            string database,
            string dbFileName,
            SqlConnection connection,
            bool sessionState,
            bool isInstall,
            SessionStateType sessionStatetype
            )
        {
            string          fullpath = Path.Combine(System.Web.HttpRuntime.AspInstallDirectory, file);
            string          content = File.ReadAllText(fullpath);
            StringReader    sr;
            string          cmdText = null;
            string          cur;
            SqlCommand      sqlCmd;

            // We need to replace the name of the database with the one specified by the caller
            if( file.Equals( INSTALL_COMMON_SQL ) )
            {
                content = FixContent(content, database, dbFileName, sessionState, sessionStatetype);
            }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -