C # Executing a DDL command

0

I'm trying to run the DDL command below that creates the table I need, but even not returning error the table is not created, would anyone have any idea where I'm going wrong?

public const string scriptCriacao =  " if exists(select * from sys.objects as o where o.type =N'u' and o.name = N'Tuss_temp')"+
" drop table Tuss_temp "+
" Create Table Tuss_temp ( "+
" competencia         nvarchar(30), "+
" codigoTerminologia  float, "+
" nomeTerminologia    nvarchar(256), "+
" codigoTermo nvarchar(30), "+
" termo nvarchar(256), "+
" dtInicioVigencia datetime, "+
" dtFimVigencia datetime, "+
" dtFimImplementacao datetime, "+
" tipoAcao nvarchar(256), " +
")";

String connectionString = "Data Source=1.1.1.1;Initial Catalog=PortalTISS_v3;Persist Security Info=True;User ID=sa;Password=teste";

SqlConnection SqlConn = new SqlConnection(connectionString);
SqlConn.Open();

SqlCommand cmd = new SqlCommand(scriptCriacao, SqlConn);
cmd.CommandType = CommandType.Text; 
cmd.ExecuteNonQuery();
    
asked by anonymous 10.01.2017 / 20:02

1 answer

0

I solved the problem by separating the script in two as seen below.

Note: The constant with the table name has no relation to the problem in question.  Ismael in the comments did not have the problem to run the composite script, I believe it may be the version of visual studio / C # I am using and / or the version of sql server I want to use.

Code.:

public const string NomeTabela = "Tuss_temp";

public const string scriptDrop = " if exists(select * from sys.objects as o where o.type =N'u' and o.name = N'"+ NomeTabela + "')" + " drop table " + NomeTabela;

public const string scriptCreate = " Create Table " + NomeTabela + " ( " +
    " competencia         nvarchar(30), " +
    " codigoTerminologia  float, " +
    " nomeTerminologia    nvarchar(256), " +
    " codigoTermo nvarchar(30), " +
    " termo nvarchar(256), " +
    " dtInicioVigencia datetime, " +
    " dtFimVigencia datetime, " +
    " dtFimImplementacao datetime, " +
    " tipoAcao nvarchar(256), " +
    ")";
    
25.01.2017 / 13:00