本文整理汇总了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);
}
}
示例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;
}