Wednesday, January 30, 2013

RUN SQL SCRIPTS USING BATCH FILE IN C#.NET


//This method creates a batch file in given location for given db credentials. Here script1.sql is sql script file.
 private void createbatch(string filepath,string filename,string dbname,string dbserver,string dbuser,string dbpwd)
        {
            string scriptfile1="\"script1.sql\"";
            string path = filepath + "\\" + filename;
            string text = @"@echo off";
            text += "\r\n";
            text += "\r\n";
            text += @"REM  **** start: database configuration details where you would like to create views ***** ";
            text += "\r\n";
            text += @"SET PMVIEWS_DATABASE=" + dbname;
            text += "\r\n";
            text += @"SET PMVIEWS_DB_SERVER=" + dbserver;
            text += "\r\n";
            text += @"SET PMVIEWS_DB_USER=" + dbuser;
            text += "\r\n";

            text += @"SET PMVIEWS_DB_PWD=" + dbpwd;
            text += "\r\n";
            text += @"REM  **** end: database configuration details where you would like to create views ***** ";
            text += "\r\n";
            text += "\r\n";
            text += @"Sqlcmd -S %PMVIEWS_DB_SERVER% -d %PMVIEWS_DATABASE%  -U %PMVIEWS_DB_USER% -P %PMVIEWS_DB_PWD% -i " + scriptfile1 + "";
            text += "\r\n";
            using (StreamWriter strwr = File.AppendText(path))
            {
                strwr.WriteLine(text);
                strwr.Flush();
                strwr.Close();
            }
        }

//This method runs given batch file. Make sure that this batch file and sql script file contains in same folder.
        private void ExecuteBatch(string strFilePath)
        {
            string fullPath = Directory.GetParent(strFilePath).FullName;
            // Create the ProcessInfo object
            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardError = true;
            psi.WorkingDirectory = fullPath;// strPath;

            // Start the process
            System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
            // Open the batch file for reading
            System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);
            // Attach the output for reading
            System.IO.StreamReader sOut = proc.StandardOutput;
            // Attach the in for writing
            System.IO.StreamWriter sIn = proc.StandardInput;
            // Write each line of the batch file to standard input
            while (strm.Peek() != -1)
            {
                sIn.WriteLine(strm.ReadLine());
            }
            strm.Close();
            // Exit CMD.EXE
            string stEchoFmt = "# {0} run successfully. Exiting";
            sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
            sIn.WriteLine("EXIT");
            // Close the process
            proc.Close();
            //this.AfterInstall += new InstallEventHandler(ServiceInstaller_AfterInstall);
            sIn.Close();
            sOut.Close();
        }