private void button2_Click(object sender, EventArgs e)
 {
     Connect_to_sql conn = new Connect_to_sql();
     string connect_string = "Data Source=" + comboBox1.Text + ";";
     connect_string += "Database=" + comboBox2.Text + ";";
     if (textBox1.Text != "")
     {
         connect_string += "Integrated Security=false;";
         connect_string += "User ID=" + textBox1.Text + ";";
         connect_string += "Password="******";";
     }
     else
     {
         connect_string += "Integrated Security=true;";
     }
     //string connect_string = conn.Con_str();
     if (comboBox1.Text!="" && comboBox2.Text!="")
     {
         if (saveFileDialog1.ShowDialog() == DialogResult.OK)
         {
             string query_backup = "BACKUP DATABASE " + comboBox2.SelectedItem.ToString() + " TO DISK = '" + saveFileDialog1.FileName + "'";
             try
             {
                 conn.ExecQuery(connect_string, query_backup);
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message);
             }
         }
     }
 }
        private void button3_Click(object sender, EventArgs e)
        {
            Connect_to_sql conn = new Connect_to_sql();
            string connect_string = "Data Source=" + comboBox1.Text + ";";
            connect_string += "Database=" + comboBox2.Text + ";";
            if (textBox1.Text != "")
            {
                connect_string += "Integrated Security=false;";
                connect_string += "User ID=" + textBox1.Text + ";";
                connect_string += "Password="******";";
            }
            else
            {
                connect_string += "Integrated Security=true;";
            }
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string query_log_name = "RESTORE FILELISTONLY FROM DISK = '" + openFileDialog1.FileName + "'";
                SqlConnection myConnection = new SqlConnection(connect_string);
                string myInsertQuery = query_log_name;
                SqlCommand myCommand = new SqlCommand(myInsertQuery);
                myCommand.Connection = myConnection;
                string[] DB_name = new string[2];
                string[] DB_path = new string[2];
                string[] DB_new_path = new string[2];
                try
                {
                    myConnection.Open();
                    SqlDataReader reader = myCommand.ExecuteReader();

                    int kk = 0;
                    int pos = 0;
                    while (reader.Read())
                    {
                        DB_name[kk] = Convert.ToString(reader[0]);
                        DB_path[kk] = Convert.ToString(reader[1]);
                        kk++;
                    }
                    for (int i = 0; i < 2; i++)
                    {
                        for (int j = DB_path[i].Length - 1; j > 0; j--)
                        {
                            if (DB_path[i][j] == '\\')
                            {
                                DB_new_path[i] = DB_path[i].Substring(0, j);
                                break;
                            }
                        }
                    }
                    myCommand.Connection.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                string query_backup = "RESTORE DATABASE " + comboBox2.SelectedItem.ToString() + " FROM DISK = '" + openFileDialog1.FileName + "' WITH REPLACE, MOVE '" + DB_name[0] + "' TO '" + DB_new_path[0] + "\\" + comboBox2.SelectedItem.ToString() + ".mdf', MOVE '" + DB_name[1] + "' TO '" + DB_new_path[1] + "\\" + comboBox2.SelectedItem.ToString() + "_Log.ldf'";
                try
                {
                    conn.ExecQuery(connect_string, query_backup);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        private void button7_Click(object sender, EventArgs e)
        {
            string first = "Data Source=" + comboBox3.Text + ";";
            if (comboBox1.Text == "Да")
            {
                first = first + "AttachDbFilename=" + textBox2.Text + ";";
            }
            //first = first + "Database=master;";
            if (comboBox2.Text != "Аутентификация Windows")
            {
                first = first + "Integrated Security=false;";
                first = first + "User ID=" + textBox4.Text + ";";
                first = first + "Password="******";";
            }
            else
            {
                first = first + "Integrated Security=true;";
            }

            //f_closed = false;
               // button2_Click(button2, e);
            /*
            Form_save_db f_save = new Form_save_db();
            f_save.Owner = this;
            f_save.con_string = first;
            f_save.ShowDialog();*/
                // If the user has chosen the file from which he wants the database to be restored

            Connect_to_sql conn = new Connect_to_sql();

                if (openFileDialog2.ShowDialog() == DialogResult.OK)
                {
                    string query_log_name = "RESTORE FILELISTONLY FROM DISK = '" + openFileDialog2.FileName + "'";

                    string connect_string = conn.Con_str();
                    SqlConnection myConnection = new SqlConnection(first);
                    string myInsertQuery = query_log_name;
                    SqlCommand myCommand = new SqlCommand(myInsertQuery);
                    myCommand.Connection = myConnection;
                    string[] DB_name = new string[2];
                    string DB_path = "";
                    string query_path = "select filename from sysfiles where name='master'";
                    try
                    {
                        myConnection.Open();
                        SqlDataReader reader = myCommand.ExecuteReader();

                        int kk = 0;
                        int pos=0;
                        while (reader.Read())
                        {
                            DB_name[kk] = Convert.ToString(reader[0]);
                            kk++;
                        }
                        myCommand.Connection.Close();
                        myCommand = new SqlCommand(query_path);
                        myCommand.Connection = myConnection;
                        myConnection.Open();
                        DB_path = Convert.ToString(myCommand.ExecuteScalar());
                            for (int j = DB_path.Length-1; j > 0; j--)
                            {
                                if (DB_path[j] == '\\')
                                {
                                    DB_path = DB_path.Substring(0, j);
                                    break;
                                }
                            }

                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }

                    string query_backup = "RESTORE DATABASE " + textBox1.Text + " FROM DISK = '" + openFileDialog2.FileName + "' WITH REPLACE, MOVE '" + DB_name[0] + "' TO '" + DB_path + "\\" + textBox1.Text + ".mdf', MOVE '" + DB_name[1] + "' TO '" + DB_path + "\\" + textBox1.Text + "_Log.ldf'";
                    try
                    {
                        conn.ExecQuery(first, query_backup);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
        }
Exemple #4
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Connect_to_sql conn = new Connect_to_sql();
                string connect_string = conn.Con_str();
                TextBox[] stolbec1 = GetTextBoxVesVlazhPochvi();
                TextBox[] stolbec2 = GetTextBoxVesSuhojPochvi();
                TextBox[] stolbec3 = GetTextBoxVesStakan();
                TextBox[] stolbec4 = GetTextBoxPole4();
                TextBox[] stolbec5 = GetTextBoxPole5();
                TextBox[] stolbec6 = GetTextBoxPole6();
                TextBox[] stolbec7 = GetTextBoxPole7();
                for (int i = 0; i < 11; i++)
                {
                    stolbec1[i].Text = stolbec1[i].Text.Replace('.', ',');
                    stolbec2[i].Text = stolbec2[i].Text.Replace('.', ',');
                    stolbec3[i].Text = stolbec3[i].Text.Replace('.', ',');
                }
                if (!ras_param.sloj0_5)
                {
                    for (int i = 1; i < 11; i++)
                    {
                        stolbec4[i].Text = Convert.ToString(conn.RoundTo((Convert.ToDouble(stolbec1[i].Text) - Convert.ToDouble(stolbec2[i].Text)), 1));
                    }
                    for (int i = 1; i < 11; i++)
                    {
                        stolbec5[i].Text = Convert.ToString(conn.RoundTo((Convert.ToDouble(stolbec2[i].Text) - Convert.ToDouble(stolbec3[i].Text)), 1));
                    }
                    for (int i = 1; i < 11; i++)
                    {
                        stolbec6[i].Text = Convert.ToString(conn.RoundTo(((Convert.ToDouble(stolbec4[i].Text) / Convert.ToDouble(stolbec5[i].Text)) * 100), 1));
                    }
                    for (int i = 1; i < 11; i++)
                    {
                        stolbec7[i].Text = Convert.ToString(conn.RoundTo((Convert.ToDouble(stolbec1[i].Text) - Convert.ToDouble(stolbec3[i].Text)), 1));
                    }
                }
                else
                {
                    for (int i = 0; i < 11; i++)
                    {
                        stolbec4[i].Text = Convert.ToString(conn.RoundTo((Convert.ToDouble(stolbec1[i].Text) - Convert.ToDouble(stolbec2[i].Text)), 1));
                    }
                    for (int i = 0; i < 11; i++)
                    {
                        stolbec5[i].Text = Convert.ToString(conn.RoundTo((Convert.ToDouble(stolbec2[i].Text) - Convert.ToDouble(stolbec3[i].Text)), 1));
                    }
                    for (int i = 0; i < 11; i++)
                    {
                        stolbec6[i].Text = Convert.ToString(conn.RoundTo(((Convert.ToDouble(stolbec4[i].Text) / Convert.ToDouble(stolbec5[i].Text)) * 100), 1));
                    }
                    for (int i = 0; i < 11; i++)
                    {
                        stolbec7[i].Text = Convert.ToString(conn.RoundTo((Convert.ToDouble(stolbec1[i].Text) - Convert.ToDouble(stolbec3[i].Text)), 1));
                    }
                }

                Connect_to_sql.AgroDataPovt[] DataPovt = new Connect_to_sql.AgroDataPovt[11];
                for (int i = 0; i < 11; i++)
                {
                    DataPovt[i].ves_vlazhnoj = ConvertStringToFloat(stolbec1[i].Text);
                    DataPovt[i].ves_suhoj = ConvertStringToFloat(stolbec2[i].Text);
                    DataPovt[i].ves_stakan = ConvertStringToFloat(stolbec3[i].Text);
                    DataPovt[i].vlazhn_suhaya = ConvertStringToFloat(stolbec4[i].Text);
                    DataPovt[i].suhaya_stakan = ConvertStringToFloat(stolbec5[i].Text);
                    DataPovt[i].procent_isparivsh_vlagi = ConvertStringToFloat(stolbec6[i].Text);
                    DataPovt[i].vlazhnaya_stakan = ConvertStringToFloat(stolbec7[i].Text);
                }

                string query_progress = "SELECT progress FROM t_Glavnaya WHERE kod=" + ras_param.Glav_ID;
                if (!ras_param.marshrut)
                {
                    if (((int)comboBox1.SelectedItem == comboBox1.Items.Count) && ((int)comboBox1.SelectedItem <= 4))
                    {
                        int progress = conn.ExecQueryWithResult(connect_string, query_progress);
                        if (progress == 4)
                        {
                            string query_delRowFrom = "DELETE FROM t_data_povt WHERE kod_Glavn=" + ras_param.Glav_ID + " AND Povtornost=" + (int)comboBox1.SelectedItem;
                            conn.ExecQuery(connect_string, query_delRowFrom);
                        }
                        conn.InsertRowToDataPovt(ras_param.Glav_ID, (int)comboBox1.SelectedItem, DataPovt, ras_param.sloj0_5, textBox134.Text, connect_string);

                        string query_edit_progress = "UPDATE t_Glavnaya SET progress=" + comboBox1.Items.Count + " WHERE kod=" + ras_param.Glav_ID;
                        conn.ExecQuery(connect_string, query_edit_progress);
                        if (comboBox1.Items.Count < 4)
                        {
                            comboBox1.Items.Add((int)comboBox1.SelectedItem + 1);
                        }
                    }
                    else
                    {
                        string query_delRowFrom = "DELETE FROM t_data_povt WHERE kod_Glavn=" + ras_param.Glav_ID + " AND Povtornost=" + (int)comboBox1.SelectedItem;
                        conn.ExecQuery(connect_string, query_delRowFrom);
                        conn.InsertRowToDataPovt(ras_param.Glav_ID, (int)comboBox1.SelectedItem, DataPovt, ras_param.sloj0_5, textBox134.Text, connect_string);
                    }
                }
                else
                {
                    if (((int)comboBox1.SelectedItem == comboBox1.Items.Count) && ((int)comboBox1.SelectedItem <= 2))
                    {
                        int progress = conn.ExecQueryWithResult(connect_string, query_progress);
                        if (progress == 2)
                        {
                            string query_delRowFrom = "DELETE FROM t_data_povt WHERE kod_Glavn=" + ras_param.Glav_ID + " AND Povtornost=" + (int)comboBox1.SelectedItem;
                            conn.ExecQuery(connect_string, query_delRowFrom);
                        }
                        conn.InsertRowToDataPovt(ras_param.Glav_ID, (int)comboBox1.SelectedItem, DataPovt, ras_param.sloj0_5, textBox134.Text, connect_string);
                        // int progress = conn.ExecQueryWithResult(connect_string, query_progress);
                        string query_edit_progress = "UPDATE t_Glavnaya SET progress=" + comboBox1.Items.Count + " WHERE kod=" + ras_param.Glav_ID;
                        conn.ExecQuery(connect_string, query_edit_progress);
                        if (comboBox1.Items.Count < 2)
                        {
                            comboBox1.Items.Add((int)comboBox1.SelectedItem + 1);
                        }
                    }
                    else
                    {
                        string query_delRowFrom = "DELETE FROM t_data_povt WHERE kod_Glavn=" + ras_param.Glav_ID + " AND Povtornost=" + (int)comboBox1.SelectedItem;
                        conn.ExecQuery(connect_string, query_delRowFrom);
                        conn.InsertRowToDataPovt(ras_param.Glav_ID, (int)comboBox1.SelectedItem, DataPovt, ras_param.sloj0_5, textBox134.Text, connect_string);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Ошибка!!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void button6_Click(object sender, EventArgs e)
        {
            string first = "Data Source=" + comboBox3.Text + ";";
            if (comboBox1.Text == "Да")
            {
                first = first + "AttachDbFilename=" + textBox2.Text + ";";
            }
            //first = first + "Database=master;";
            if (comboBox2.Text != "Аутентификация Windows")
            {
                first = first + "Integrated Security=false;";
                first = first + "User ID=" + textBox4.Text + ";";
                first = first + "Password="******";";
            }
            else
            {
                first = first + "Integrated Security=true;";
            }
            string query_createDB = "CREATE DATABASE " + textBox1.Text;
            Connect_to_sql conn = new Connect_to_sql();
            try
            {
                conn.ExecQuery(first, query_createDB);
                button4_Click(button4, e);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }