public void GenerateCreateDestinationTableQueryTest() { DataTable schema; using (var cn = new SqlConnection(Jhu.Graywulf.Test.AppSettings.IOTestConnectionString)) { cn.Open(); var sql = "SELECT * FROM SampleData"; using (var cmd = new SqlCommand(sql, cn)) { using (var dr = cmd.ExecuteReader(CommandBehavior.SchemaOnly | CommandBehavior.KeyInfo)) { schema = dr.GetSchemaTable(); } } } var dest = new Table() { SchemaName = "dbo", TableName = "destination" }; var cg = new SqlServerCodeGenerator(); var res = cg.GenerateCreateDestinationTableQuery(schema, dest); Assert.AreEqual(@"CREATE TABLE [dbo].[destination] ([float] real NULL, [double] float NULL, [decimal] money NULL, [nvarchar(50)] nvarchar(50) NULL, [bigint] bigint NULL, [int] int NOT NULL, [tinyint] tinyint NULL, [smallint] smallint NULL, [bit] bit NULL, [ntext] nvarchar(max) NULL, [char] char(1) NULL, [datetime] datetime NULL, [guid] uniqueidentifier NULL)", res); }
/// <summary> /// Creates the destination table /// </summary> /// <param name="schemaTable"></param> protected void CreateDestinationTable(DataTable schemaTable) { // Generate create table SQL var cg = new SqlServerCodeGenerator(); var sql = cg.GenerateCreateDestinationTableQuery(schemaTable, destination.Table); // Execute CREATE TABLE query on destination using (var cn = new SqlConnection(destination.Table.Dataset.ConnectionString)) { cn.Open(); using (var cmd = new SqlCommand(sql, cn)) { cmd.ExecuteNonQuery(); } } }