I'm doing a program that does bakcup in mySQL database. I have a screen where or soon with server, user, password and port used in the database.
But I do not want to be typing every time I run the program, so I need the program to store some data by default after the first run of it where I typed all the data.
Data I want to store by default:
- server
- user
- port
That is, the first time I start the program, I type everything: server, user, port, and password.
The second time I run the program, I want to have to type only the password, without having to type everything again.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.IO.Ports;
using System.IO;
using System.IO.Compression;
namespace Sistema
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show(" Quer fazer o backup desse banco ?\n\n" + comboBox1.Text + "\n\n Isso pode levar um tempo", "Banco selecionado!", MessageBoxButtons.YesNo);
switch (dr)
{
case DialogResult.Yes:
string nomesever = (string)sever.Text;
string nomeuser = (string)user.Text;
string senha = (string)pass.Text;
if (!Directory.Exists(caminho.Text + @"\Dumps\Arquivo_SQL"))
Directory.CreateDirectory(caminho.Text + @"\Dumps\Arquivo_SQL");
if (!Directory.Exists(caminho.Text + @"\Dumps\Arquivo_ZIP"))
Directory.CreateDirectory(caminho.Text + @"\Dumps\Arquivo_ZIP");
var currentDateTime = DateTime.Now;
string data = DateTime.Now.ToString("dd-MM-yyyy");
{
string file = caminho.Text + @"\Dumps\Arquivo_SQL\" + comboBox1.Text + data + ".sql";//cria sql
using (MySqlConnection cn = new MySqlConnection())
{
cn.ConnectionString = ("server=" + nomesever + ";Port = " + porta.Text + "; pwd = " + senha + "; database = " + comboBox1.Text + "; uid=" + nomeuser + ";SslMode = none");
using (MySqlCommand cmd = new MySqlCommand())
{
using (MySqlBackup mb = new MySqlBackup(cmd))
{
string fazendo = "fazendo backup";
MessageBox.Show(fazendo);
cmd.Connection = cn;
cn.Open();
mb.ExportToFile(file);
cn.Close();
string startPath = caminho.Text + @"\Dumps\Arquivo_SQL";
string zipPath = caminho.Text + @"\Dumps\Arquivo_ZIP\" + comboBox1.Text + data + ".zip";
ZipFile.CreateFromDirectory(startPath, zipPath);
}
}
}
}
MessageBox.Show("backup feito");
if (File.Exists(caminho.Text + @"\Dumps\Arquivo_SQL\" + comboBox1.Text + data + ".sql"))//exclue sql
File.Delete(caminho.Text + @"\Dumps\Arquivo_SQL\" + comboBox1.Text + data + ".sql");
break;
case DialogResult.No:
break;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string nomesever = (string)sever.Text;
string nomeuser = (string)user.Text;
string senha = (string)pass.Text;
try
{
MySqlConnection cn = new MySqlConnection();
cn.ConnectionString = ("server=" + nomesever + "; Port=" + porta.Text + " ; pwd=" + senha + "; uid=" + nomeuser + "; SslMode=none");
cn.Open();
MySqlCommand com = new MySqlCommand();
com.Connection = cn;
com.CommandText = "Show databases";
MySqlDataReader dr = com.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
comboBox1.DisplayMember = "Database";
comboBox1.DataSource = dt;
cn.Close();
MessageBox.Show("conectado, Selecione o banco");
}
catch
{
MessageBox.Show("Não conectado! \n verifique se os dados estão corretos.");
}
}
private void clear_Click(object sender, EventArgs e)
{
sever.Text = "";
user.Text = "";
pass.Text = "";
porta.Text = "";
comboBox1.Text = "";
caminho.Text = "";
}
private void Busca_Click(object sender, EventArgs e)
{
FolderBrowserDialog busca = new FolderBrowserDialog();
busca.RootFolder = Environment.SpecialFolder.Desktop;
busca.Description = "Selecione a pasta desejada para armazear o backup do banco de dados";
busca.ShowNewFolderButton = false;
if (busca.ShowDialog() == DialogResult.OK)
{
caminho.Text = busca.SelectedPath;
}
BT_Backup.Visible = true;
}
private void porta_TextChanged(object sender, EventArgs e)
{
}
}
}