Hi Wyatt
Thanks for replying me.
I did your suggestion, but the %~dp0 is still giving me the application path and not the temporary folder where the sql script is located. I believe is because the cmd batch file is generated as well in the application folder
Look what I have done so far in the exe application:
if (File.Exists("tempBatchV11714.cmd")) { File.Delete("tempBatchV11714.cmd"); }
//Then I create the 'cmd' file, but for some reason is generated in the application directory
using (StreamWriter sw = File.CreateText("tempBatchV11714.cmd")) { sw.WriteLine("sqlcmd -U " + sqlServerUser + " -P " + sqlServerPwd + " -S " + sqlServer + " -i \""+aTempFolder+"\\changesV11714.sql\""); sw.WriteLine("pause"); sw.WriteLine("exit /B %ERRORLEVEL%"); //Tested before // --> Tested before --> sw.WriteLine("sqlcmd -U "+sqlServerUser+" -P "+sqlServerPwd+" -S "+sqlServer+" -i \"%~dp0changesV11714.sql\""); //--> Tested before --> sw.WriteLine("sqlcmd -U " + sqlServerUser + " -P " + sqlServerPwd + " -S " + sqlServer + " -i \"%temp%\\changesV11714.sql\""); }
in the case of "+aTempFolder+", is a function parameter. This variable is having the value passed from WyBuild with %temp%.
The complete funtion is as follow:
private static void apply247libWsV11714(string aAppDir, string aTempFolder) { XmlDocument xmlDoc = GetWebConfig(); XmlNode xmlRootNode = xmlDoc.SelectSingleNode("//configuration"); if (xmlRootNode == null) { throw new Exception("configuration node in web.config not found!"); }
XmlNode connectionStringNode = xmlDoc.SelectSingleNode("/configuration/connectionStrings[last()]"); if (!connectionStringNode.HasChildNodes) throw new Exception("Cannot find connection string in configuraton file web.config of 247lib Web Service!"); XmlNode connectionStrLocalNode = connectionStringNode.SelectSingleNode("add[@name='LocalConnStr']"); string connectionStrLocal = connectionStrLocalNode.Attributes["connectionString"].Value;
string[] spplitedConnString = connectionStrLocal.Split(new char[] {';'}, StringSplitOptions.RemoveEmptyEntries); string sqlServer = string.Empty; string sqlServerUser = string.Empty; string sqlServerPwd = string.Empty; string sqlServerDataBase = string.Empty; foreach (string strConnPart in spplitedConnString) { if ((strConnPart.ToUpper().IndexOf("DATA SOURCE") > -1) || (strConnPart.ToUpper().IndexOf("SERVER") > -1)) sqlServer = strConnPart.Split(new char[] {'='}, StringSplitOptions.RemoveEmptyEntries)[1]; if ((strConnPart.ToUpper().IndexOf("USER ID") > -1) || (strConnPart.ToUpper().IndexOf("UID") > -1)) sqlServerUser = strConnPart.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries)[1]; if ((strConnPart.ToUpper().IndexOf("PWD") > -1) || (strConnPart.ToUpper().IndexOf("PWD") > -1)) sqlServerPwd = strConnPart.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries)[1]; if ((strConnPart.ToUpper().IndexOf("INITIAL CATALOG") > -1) || (strConnPart.ToUpper().IndexOf("DATABASE") > -1)) sqlServerDataBase = strConnPart.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries)[1]; }
if (File.Exists("tempBatchV11714.cmd")) { File.Delete("tempBatchV11714.cmd"); }
using (StreamWriter sw = File.CreateText("tempBatchV11714.cmd")) { sw.WriteLine("sqlcmd -U " + sqlServerUser + " -P " + sqlServerPwd + " -S " + sqlServer + " -i \""+aTempFolder+"\\changesV11714.sql\""); sw.WriteLine("pause"); sw.WriteLine("exit /B %ERRORLEVEL%"); }
ProcessStartInfo startInfo = new ProcessStartInfo(); //startInfo.FileName = "sqlcmd"; startInfo.FileName = aAppDir+"tempBatchV11714.cmd"; //--> I HAD TO SPECIFY THE APP FOLDER FOR EXECUTIN CMD startInfo.ErrorDialog = false; //startInfo.ErrorDialog = true; startInfo.UseShellExecute = false; //startInfo.WorkingDirectory = Directory.GetCurrentDirectory(); string[] args = new string[] { "-U", sqlServerUser, "-P", sqlServerPwd, "-S", sqlServer, "-d", sqlServerDataBase, "-i", Directory.GetCurrentDirectory() + "\\changesV11714.sql" }; //string[] quoted = Array.ConvertAll<string, string>(args, delegate(string input) { return string.Format("\"{0}\"", input); }); //startInfo.Arguments = string.Join(" ", args); if (!System.Diagnostics.EventLog.SourceExists("wyUpdateTempExecution")) { System.Diagnostics.EventLog.CreateEventSource("wyUpdateTempExecution", "wyUpdateTempExecution"); } System.Diagnostics.EventLog.WriteEntry("wyUpdateTempExecution", "Begining process for sql script changesV11714.sql - Process:" + startInfo.FileName, EventLogEntryType.Information);
Process process; try { process = Process.Start(startInfo); if (process == null) { //startInfo.EnvironmentVariables["Path"] = pathEnviroment; System.Diagnostics.EventLog.WriteEntry("wyUpdateTempExecution", "Error starting process from executable file Web247AppConfigMerge.exe - Exit Code 1001", EventLogEntryType.Error); //Clipboard.SetText("Error starting process from executable file Web247AppConfigMerge.exe - Exit Code 1001"); throw new Exception("Error starting process from executable file Web247AppConfigMerge.exe - Exit Code 1001"); } if (process.WaitForExit(1000 * 60)) { if (process.ExitCode != 0) { System.Diagnostics.EventLog.WriteEntry("wyUpdateTempExecution", "Error starting process from executable file Web247AppConfigMerge.exe - Exit Code 1002 - Process Exit Code " + process.ExitCode, EventLogEntryType.Error); //Clipboard.SetText("Error starting process from executable file Web247AppConfigMerge.exe - Exit Code 1002"); throw new Exception("Error starting process from executable file Web247AppConfigMerge.exe - Exit Code 1002- Process Exit Code " + process.ExitCode); } //startInfo.EnvironmentVariables["Path"] = pathEnviroment; if (File.Exists("tempBatchV11714.cmd")) { File.Delete("tempBatchV11714.cmd"); } return; } process.CloseMainWindow(); if (!process.WaitForExit(3000)) { process.Kill(); } //startInfo.EnvironmentVariables["Path"] = pathEnviroment; System.Diagnostics.EventLog.WriteEntry("wyUpdateTempExecution", "Error starting process from executable file Web247AppConfigMerge.exe - Exit Code 1003", EventLogEntryType.Error); //Clipboard.SetText("Error starting process from executable file Web247AppConfigMerge.exe - Exit Code 1003"); throw new Exception("Error starting process from executable file Web247AppConfigMerge.exe - Exit Code 1003");
} catch (Exception exc) { // TODO: log exception //startInfo.EnvironmentVariables["Path"] = pathEnviroment; System.Diagnostics.EventLog.WriteEntry("wyUpdateTempExecution", exc.Message, EventLogEntryType.Error); //Clipboard.SetText(exc.Message); throw new Exception(exc.Message); }
}