0
I’m making a bakcup program in mysql database. I have a screen where or logo with server, user, password and port used in the bank.
But I don’t want to keep 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 wish to store by default:
- server
- user
- door
That is, the first time I start the program, I type everything:server, user, port and password.
Already the second time I run the program, I want to have to enter 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)
{
}
}
}
Your question is very wide, but to give you a north, you can save the data in a file
.txt
or.ini
(recommended).– Roberto de Campos
http://www.codigoexpresso.com.br/Home/Postagem/24
– Roberto de Campos
would not be the case to leave the connection string ready in an app.Settings or config?
– Leandro Angelo