当前位置: 首页>>代码示例>>C#>>正文


C# SqlDatabase.Execute方法代码示例

本文整理汇总了C#中SqlDatabase.Execute方法的典型用法代码示例。如果您正苦于以下问题:C# SqlDatabase.Execute方法的具体用法?C# SqlDatabase.Execute怎么用?C# SqlDatabase.Execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SqlDatabase的用法示例。


在下文中一共展示了SqlDatabase.Execute方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Initialize

        public void Initialize(SqlDatabase sqlDatabase)
        {
            Guard.Against.Null(() => sqlDatabase);

            var versionRepository = new VersionRepository(sqlDatabase.ConnectionString);

            var version = 0;
            try
            {
                version = versionRepository.GetVersion(this.databaseName, this.schemaName);
            }
            catch (SqlException ex)
            {
                if (!ex.Errors.Cast<SqlError>().Any(error => error.Number == 2812))
                {
                    throw;
                }
            }

            var sqlScripts = this.scriptManager.GetSqlScripts(this.databaseName);
            if (!sqlScripts.Any())
            {
                this.scriptManager.ThrowMissingScriptException(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Cannot find any SQL scripts for the database named '{0}'.",
                        this.databaseName));
            }

            if (sqlScripts.Any(sqlScript => sqlScript == null))
            {
                throw new ArgumentException("One or more of the scripts returned from the script manager has a null value.");
            }

            if (sqlScripts.Min(script => script.Version != 1))
            {
                this.scriptManager.ThrowMissingScriptException(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "Cannot find version one of the SQL script for the database named '{0}'.",
                        this.databaseName));
            }

            // LINK (Cameron): http://stackoverflow.com/questions/13359327/check-if-listint32-values-are-consecutive
            if (sqlScripts.Select(script => script.Version).Select((v1, v2) => v1 - v2).Distinct().Skip(1).Any())
            {
                this.scriptManager.ThrowMissingScriptException(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "The SQL scripts for the database named '{0}' are non-contiguous.",
                        this.databaseName));
            }

            var lastSqlScript = sqlScripts.Last();
            if (lastSqlScript.Version == version)
            {
                return;
            }

            if (lastSqlScript.Version < version)
            {
                var exception = (SqlException)FormatterServices.GetUninitializedObject(typeof(SqlException));
                typeof(SqlException)
                    .GetField("_message", BindingFlags.NonPublic | BindingFlags.Instance)
                    .SetValue(
                        exception,
                        string.Format(
                            CultureInfo.InvariantCulture,
                            "The database version '{0}' is ahead of the database version '{1}' supported by this library.",
                            version,
                            lastSqlScript.Version));

                throw exception;
            }

            sqlDatabase.Execute(sqlScripts.Where(sqlScript => sqlScript.Version > version), this.schemaName);

            versionRepository.SetVersion(this.databaseName, this.schemaName, lastSqlScript.Version, lastSqlScript.Description);
        }
开发者ID:cameronfletcher,项目名称:meld,代码行数:79,代码来源:SqlDatabaseSchema.cs


注:本文中的SqlDatabase.Execute方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。