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


C# SqlConnection.GetTableNames方法代码示例

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


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

示例1: DropTable

        public static void DropTable(string tableName)
        {
            try
            {
                using (var connection = new SqlConnection(ConnectionString))
                {
                    connection.Open();

                    if (!connection.GetTableNames().Contains(tableName, StringComparer.InvariantCultureIgnoreCase)) return;

                    Console.WriteLine("Dropping table {0}", tableName);

                    try
                    {
                        using (var command = connection.CreateCommand())
                        {
                            command.CommandText = string.Format("DROP TABLE [{0}]", tableName);
                            command.ExecuteNonQuery();
                        }
                    }
                    catch (SqlException exception)
                    {
                        if (exception.Number == SqlServerMagic.ObjectDoesNotExistOrNoPermission) return;

                        throw;
                    }
                }
            }
            catch (Exception exception)
            {
                DumpWho();

                throw new ApplicationException(string.Format("Could not drop table '{0}'", tableName), exception);
            }
        }
开发者ID:netojoaop,项目名称:Rebus,代码行数:35,代码来源:SqlTestHelper.cs

示例2: EnsureTableIsCreated

        /// <summary>
        /// Creates the necessary timeout storage table if it hasn't already been created. If a table already exists
        /// with a name that matches the desired table name, no action is performed (i.e. it is assumed that
        /// the table already exists).
        /// </summary>
        public SqlServerTimeoutStorage EnsureTableIsCreated()
        {
            using (var connection = new SqlConnection(connectionString))
            {
                connection.Open();

                var tableNames = connection.GetTableNames();

                if (tableNames.Contains(timeoutsTableName, StringComparer.OrdinalIgnoreCase))
                {
                    return this;
                }

                log.Info("Table '{0}' does not exist - it will be created now", timeoutsTableName);

                using (var command = connection.CreateCommand())
                {
                    command.CommandText = string.Format(@"

CREATE TABLE [dbo].[{0}](
    [id] [bigint] IDENTITY(1,1) NOT NULL,
	[time_to_return] [datetime2](7) NOT NULL,
	[correlation_id] [nvarchar](200) NOT NULL,
	[saga_id] [uniqueidentifier] NOT NULL,
	[reply_to] [nvarchar](200) NOT NULL,
	[custom_data] [nvarchar](MAX) NULL
    CONSTRAINT [PK_{0}] PRIMARY KEY NONCLUSTERED 
    (
	    [id] ASC
    ) WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON)
)

", timeoutsTableName);
                    command.ExecuteNonQuery();
                }

                using (var command = connection.CreateCommand())
                {
                    command.CommandText = string.Format(@"

CREATE CLUSTERED INDEX [IX_{0}_TimeToReturn] ON [dbo].[{0}]
(
	[time_to_return] ASC
)

", timeoutsTableName);
                    command.ExecuteNonQuery();
                }
            }

            return this;
        }
开发者ID:nls75,项目名称:Rebus,代码行数:57,代码来源:SqlServerTimeoutStorage.cs


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