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


C# IDatabase.Escape方法代码示例

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


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

示例1: Execute

        public override void Execute(IDatabase database)
        {
            if (!Table.Columns.Any())
            {
                throw new InvalidOperationException("Cannot create a table with no columns.");
            }

            var sql = new SqlBuilder();
            sql.Append("if not ({0}) begin create table {1} (",
                GetTableExistsSql(database, Table.Name),
                database.FormatTableNameAndEscape(Table.Name));

            var i = 0;
            foreach (var column in Table.Columns)
            {
                var sqlBuilder = new SqlBuilder()
                    .Append(i > 0, ",")
                    .Append(database.Escape(column.Name))
                    .Append(GetColumnSqlType(column, i.ToString()));

                sql.Append(sqlBuilder);
                i++;
            }

            sql.Append("); end;");

            database.RawExecute(sql.ToString());
        }
开发者ID:asgerhallas,项目名称:HybridDb,代码行数:28,代码来源:CreateTable.cs

示例2: Execute

        public override void Execute(IDatabase database)
        {
            // Bug in SQL server 2012 prevents us to query SYS.DEFAULT_CONSTRAINTS for temp tables. Furthermore it is only possible to query
            // the table sys.objects$ and the value parent_object_id of sys.objects if using DAC, for which reason it is not possible to get
            // column constraints on a specific column.
            // See: https://connect.microsoft.com/SQLServer/feedback/details/765777/sys-default-constraints-empty-for-temporary-tables-in-tempdb
            // See: http://www.sqlservercentral.com/Forums/Topic1359991-3077-1.aspx
            if (database is SqlServerUsingTempTables)
                throw new InvalidOperationException("It is currently not possible to remove columns on temp tables.");

            var tableName = database.FormatTableNameAndEscape(Table.Name);
            var dropConstraints = "DECLARE @ConstraintName nvarchar(200) " +
                                  "SELECT @ConstraintName = Name FROM SYS.DEFAULT_CONSTRAINTS " +
                                  "WHERE PARENT_OBJECT_ID = OBJECT_ID('" + tableName + "') " +
                                  "AND PARENT_COLUMN_ID = (SELECT column_id FROM sys.columns WHERE NAME = N'" + Name + "' AND object_id = OBJECT_ID(N'" + tableName + "')) " +
                                  "IF @ConstraintName IS NOT NULL " +
                                  "EXEC('ALTER TABLE " + tableName + " DROP CONSTRAINT ' + @ConstraintName) ";

            //Solutions for dropping column constraints on temp tables, when either SQL server bug is fixed or DAC connection is used
            //var queryForTempTableAsItShouldBe = "DECLARE @ConstraintName nvarchar(200) " +
            //                   "SELECT @ConstraintName = Name FROM tempdb.SYS.DEFAULT_CONSTRAINTS " +
            //                   "WHERE PARENT_OBJECT_ID = OBJECT_ID(N'tempdb.." + tableName + "') " +
            //                   "AND PARENT_COLUMN_ID = (SELECT column_id FROM tempdb.sys.columns WHERE NAME = N'" + Name + "' AND object_id = OBJECT_ID(N'tempdb.." + tableName + "')) " +
            //                   "IF @ConstraintName IS NOT NULL " +
            //                   "EXEC('ALTER TABLE tempdb.." + tableName + " DROP CONSTRAINT ' + @ConstraintName) ";
            //var queryForTempTableAsAlternative = "DECLARE @ConstraintName nvarchar(200) " +
            //                   "SELECT @ConstraintName = Name FROM tempdb.sys.objects " +
            //                   "WHERE type = 'D ' AND parent_object_id <> 0 " +
            //                   "AND PARENT_COLUMN_ID = (SELECT column_id FROM tempdb.sys.columns WHERE NAME = N'" + Name + "' AND object_id = OBJECT_ID(N'tempdb.." + tableName + "')) " +
            //                   "IF @ConstraintName IS NOT NULL " +
            //                   "EXEC('ALTER TABLE tempdb.." + tableName + " DROP CONSTRAINT ' + @ConstraintName) ";

            database.RawExecute(dropConstraints);                
            database.RawExecute(string.Format("alter table {0} drop column {1};", database.FormatTableNameAndEscape(Table.Name), database.Escape(Name)));
        }
开发者ID:dcga,项目名称:HybridDb,代码行数:35,代码来源:RemoveColumn.cs

示例3: Execute

        public override void Execute(IDatabase database)
        {
            var sql = new SqlBuilder();
            sql.Append("alter table {0} add {1}", database.FormatTableNameAndEscape(Tablename), database.Escape(Column.Name));
            sql.Append(GetColumnSqlType(Column));

            database.RawExecute(sql.ToString());
        }
开发者ID:dcga,项目名称:HybridDb,代码行数:8,代码来源:AddColumn.cs

示例4: Execute

        public override void Execute(IDatabase database)
        {
            var builder = new SqlBuilder();
            var formattedAndEscapedTableName = database.FormatTableNameAndEscape(TableName);
            var escapedColumnName = database.Escape(Column.Name);

            //DROP CONSTRAINTS
            foreach (var constraintName in (database is SqlServerUsingTempTables ? tempTablesConstraintQueries : constraintQueries)
                .Select(x => database.RawQuery<string>(x, new { TableName = database.FormatTableName(TableName), ColumnName = Column.Name }))
                .Select(x => x.SingleOrDefault())
                .Where(x => x != null))
            {
                builder.Append("ALTER TABLE {0} DROP CONSTRAINT {1};", formattedAndEscapedTableName, database.Escape(constraintName));
            }

            builder
                //ALTER TABLE
                .Append(GetAlterTableStatement(formattedAndEscapedTableName, escapedColumnName))

                //DEFAULT CONSTRAINT
                .Append(Column.DefaultValue != null, "ALTER TABLE {0} ADD DEFAULT '{1}' FOR {2};", formattedAndEscapedTableName, Column.DefaultValue, escapedColumnName)

                //PRIMAY KEY
                .Append(Column.IsPrimaryKey, @"ALTER TABLE {0} ADD PRIMARY KEY ({1});", formattedAndEscapedTableName, escapedColumnName);

            database.RawExecute(builder.ToString());
        }
开发者ID:asgerhallas,项目名称:HybridDb,代码行数:27,代码来源:ChangeColumnType.cs


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