예제 #1
0
        //SqlDataReader dataReader;

        public void ExecuteScripts(string selectedOption, DataGridView dgvListProcess, ArrayList checkedDB, Label label9,
                                   BackgroundWorker bgwScript, Label label10, Server cons, RichTextBox rcProcess, Constring connects, ComboBox cboAuthentication,
                                   string selectedServer, TextBox txtUsername, TextBox txtPassword, CheckBox grantCheckBox, string constring)
        {
            BusinessFacade facade = new BusinessFacade();

            string erro = string.Empty;

            //if (flag != filePaths.Length)
            //{
            //    //ubah lstdrive ke dgvListProcess
            //    filePaths = new string[dgvListProcess.RowCount];
            //    //filePaths = new string[lstDrive.Items.Count];

            //    for (int j = 0; j < dgvListProcess.RowCount; j++)
            //    {
            //        //filePaths[j] = lstDrive.Items[j].ToString();
            //        filePaths[j] = dgvListProcess.Rows[j].ToString();
            //    }
            //}
            int point;

            switch (selectedOption)
            {
            case "all":
                startPoint = 0;
                break;

            case "one":
                point      = dgvListProcess.CurrentRow.Index;
                startPoint = point;
                endPoint   = point;
                break;

            case "from":
                point      = dgvListProcess.CurrentRow.Index;
                startPoint = point;
                break;
            }

            //else if (execOptions.SelectedIndex == null)
            //{
            //    MessageBox.Show("Please select options below first.");
            //    return;
            //}

            ////get checked dgv
            //if (dgvListProcess.SelectedRows.Count == 0)
            //{
            //    startPoint = 0;
            //}
            //else
            //{
            //    int point = dgvListProcess.CurrentRow.Index;
            //    startPoint = point;
            //}

            //initiate connection
            connection = new SqlConnection(constring);
            connection.Open();

            int i;

            for (i = startPoint; i < dgvListProcess.Rows.Count - 1; i++)
            {
                //if (i >= filePaths.Length)
                //{
                //    break;
                //}
                //Constring connects;
                foreach (string DB in checkedDB)
                {
                    label9.Invoke((MethodInvoker) delegate
                    {
                        label9.Text = DB;
                    });
                    //connect to db
                    cboAuthentication.Invoke((MethodInvoker) delegate
                    {
                        if (cboAuthentication.Items[cboAuthentication.SelectedIndex].ToString().Contains("Windows"))
                        {
                            //connects = new Constring(this.ddlServer.Text, DB);
                            connects = new Constring(selectedServer, DB);
                        }
                        else
                        {
                            //connects = new Constring(this.txtUsername.Text, this.txtPassword.Text, this.ddlServer.Text, DB);
                            connects = new Constring(txtUsername.Text, txtPassword.Text, selectedServer, DB);
                        }

                        cons = connects.Connect();
                    });

                    //facade.InsertAppLog(filePaths[i], DB);
                    facade.InsertAppLog(dgvListProcess.Rows[i].Cells[1].Value.ToString(), DB);
                    //exec script
                    this.GenerateStoredProc(dgvListProcess.Rows[i].Cells[1].Value.ToString(), ref erro, label10, cons, rcProcess, DB, selectedServer);
                    if (grantCheckBox.Checked == false)
                    {
                        //if execution fails
                        if (!string.IsNullOrEmpty(erro))
                        {
                            facade.InsertAppLog(dgvListProcess.Rows[i].Cells[1].Value.ToString(), DB, erro, grantCheckBox);
                            bgwScript.ReportProgress(i, "Error");
                            MessageBox.Show(erro);
                            return;
                        }
                    }
                    else
                    {
                        //if execution fails
                        if (!string.IsNullOrEmpty(erro))
                        {
                            facade.InsertAppLog(dgvListProcess.Rows[i].Cells[1].Value.ToString(), DB, erro, grantCheckBox);
                            bgwScript.ReportProgress(i, "Error");
                            MessageBox.Show(erro);
                            //ga berhenti jika ada error
                        }
                    }


                    //this.lstDrive.Items.RemoveAt(this.lstDrive.FindString(filePaths[i]));
                    //bgwScript.ReportProgress(i, "Success");
                    //this.dgvListProcess.Rows[i].Cells[0].Value = "Success";
                }
                bgwScript.ReportProgress(i, "Success");
                //this.flag--;
                if (i == endPoint)
                {
                    endPoint = 999999;
                    //MessageBox.Show("scripts on DB " + label9.Text + " Successfully execute");
                    return;
                }
            }
            //close connection
            connection.Close();
        }