public override bool Execute() { SqlConnection conn2 = null; //Debugger.Launch(); bool typeExist = false; string stringName = ""; string sysName = ""; try { //check that we have the necessary info Validate(); if (toConnect) { //open the connection conn = new SqlConnection(connString); conn.Open(); //start the tx tx = conn.BeginTransaction(); } //create the file if (toScript) { string deploymentString = "--Deployment script for UDT's in assembly: " + asmName; string testString = "--Testing statements script for UDT's in assembly: " + asmName; if (alterAsm) { deploymentString = "--Deployment script for new UDT's in altered assembly: " + asmName; testString = "--Testing statements script for new UDT's in altered assembly: " + asmName; } sw = Utility.OpenFile(scriptFile, !alterAsm); swRun = Utility.OpenFile(runFile, !alterAsm); Utility.WriteToFile(sw, deploymentString, false, false); Utility.WriteToFile(sw, "--Autogenerated at: " + DateTime.Now, false, true); Utility.WriteToFile(swRun, testString, false, false); Utility.WriteToFile(swRun, "--Autogenerated at: " + DateTime.Now, false, true); } if (!alterAsm) { //drop types DropAssembly d = new DropAssembly(sw, conn, tx, asmName, toDropTable, castType, toScript, this, Assembly.LoadFile(asmPath)); //drop types and dependent stuff d.DropTypes(toConnect); } else if (alterAsm) { conn2 = new SqlConnection(connString); conn2.Open(); SqlCommand cmd = conn2.CreateCommand(); cmd.CommandText = @"select t.assembly_class realname, t.name alias from sys.assembly_types t with (nolock) join sys.assemblies a with (nolock) on t.assembly_id = a.assembly_id where a.name = '" + asmName + "'"; SqlDataReader dr = cmd.ExecuteReader(); if (dr != null && dr.HasRows) { alTypes = new ArrayList(); while (dr.Read()) alTypes.Add(dr.GetString(0) + ", " + dr.GetString(1)); dr.Close(); conn2.Close(); } } Type attr = typeof(SqlUserDefinedTypeAttribute); //get the assembly Assembly asm = Assembly.LoadFile(asmPath); if (toScript) { Utility.WriteToFile(sw, "--About to create UDT's", false, false); } foreach (Type t in asm.GetTypes()) { string typeName = t.Name; //check for the UDT attribute if (t.IsDefined(attr, false)) { object[] attrs = t.GetCustomAttributes(attr,false); SqlUserDefinedTypeAttribute udt = (SqlUserDefinedTypeAttribute)attrs[0]; //string typeName = udt.Name; if(udt.Name !=string.Empty && udt.Name != null ) typeName = udt.Name; sysName = Utility.GetSysName(typeName, out stringName); string fullName = t.FullName; if (alterAsm && CheckTypeExists(t.Name, stringName, alTypes)) continue; typeExist = true; string createText = "CREATE TYPE " + sysName + "\nEXTERNAL NAME [" + asmName + "].[" + fullName + "]"; if (toScript) { Utility.WriteToFile(sw, createText, true, true); if (createText.Contains("CREATE")) Utility.WriteToFile(swRun, Utility.GetExecString(createText), false, false); } if (toConnect) { Utility.LogMyComment(this, "About to execute:\n" + createText + "\n"); Utility.WriteToDb(createText, conn, tx); Utility.LogMyComment(this, "Executed with success\n"); } } } if (typeExist) { if (toConnect) { Utility.LogMyComment(this, "Comitting Transaction"); tx.Commit(); } Utility.LogMyComment(this, "Deployment Succeeded!\n"); return true; } } catch (Exception e) { Utility.LogMyComment(this, "Error(s) Occured"); Utility.LogMyComment(this, "Creating Type(s) Failed"); Utility.LogMyErrorFromException(this, e); if (toConnect) { if (tx != null){ Utility.LogMyComment(this, "Rolling Back Transaction\n"); if(tx.Connection!=null) tx.Rollback(); } } return false; } finally { if (toConnect) { if (tx != null) { if (tx.Connection != null) tx.Rollback(); } if (conn != null && conn.State != ConnectionState.Closed) conn.Close(); } if (toScript && sw != null) { sw.Flush(); sw.Close(); } if (toScript && swRun != null) { swRun.Flush(); swRun.Close(); } } return true; }
public override bool Execute() { //Debugger.Launch(); try { //check that we have the necessary info Validate(); if (toConnect) { //open the connection conn = new SqlConnection(connString); conn.Open(); //start the tx tx = conn.BeginTransaction(); } //create the file if (toScript) { string deploymentString = "--Deployment script for assembly: " + asmName; if (alterAsm) deploymentString = "--Deployment script for alter assembly: " + asmName; sw = Utility.OpenFile(scriptFile, !alterAsm); Utility.WriteToFile(sw, deploymentString, false, false); Utility.WriteToFile(sw, "--Autogenerated at: " + DateTime.Now, false, true); } //if this is alter, do not drop dependents if (!alterAsm) { DropAssembly d = new DropAssembly(sw, conn, tx, asmName, toDropTable, castType, toScript, this, Assembly.LoadFile(asmPath)); d.DropAsm(toConnect); } //if we are using the VS project type, check if deployproperties exist if(useDMDeployAttr) DeployAttributeDll(); List<DMAssemblyName> asl = DeployAsm(); if (deployDebug) DeploySymbols(asl); if (toUpload) DeployFiles(); if (toConnect) { Utility.LogMyComment(this, "Comitting Transaction"); tx.Commit(); } Utility.LogMyComment(this, "Deployment Succeeded!\n"); return true; } catch (Exception e) { Utility.LogMyComment(this, "Error(s) Occured"); Utility.LogMyComment(this, "Deployment Failed"); Utility.LogMyErrorFromException(this, e); if (toConnect) { if (tx != null) { Utility.LogMyComment(this, "Rolling Back Transaction"); if (tx.Connection != null) tx.Rollback(); } } return false; } finally { if (toConnect) { if (conn != null && conn.State != ConnectionState.Closed) conn.Close(); } if (toScript && sw != null) { sw.Flush(); sw.Close(); } } }