-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormCadastro.cs
More file actions
87 lines (76 loc) · 3.04 KB
/
Copy pathFormCadastro.cs
File metadata and controls
87 lines (76 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace Ajuda.AI
{
public partial class FormCadastro : Form
{
private string ConnectionString = @"Data Source=DESKTOP-LUIZGAB\SQLEXPRESS02;Initial Catalog=AjudaAIDb;Integrated Security=True";
public FormCadastro()
{
InitializeComponent();
}
private void BtnCadastrar_Click(object sender, EventArgs e)
{
string nome = txtNome.Text.Trim();
string email = txtEmail.Text.Trim();
string senha = txtSenha.Text.Trim();
if (string.IsNullOrEmpty(nome) || string.IsNullOrEmpty(email) || string.IsNullOrEmpty(senha))
{
MessageBox.Show("Preencha todos os campos!");
return;
}
if (!email.Contains("@") || !email.Contains("."))
{
MessageBox.Show("E-mail inválido!");
return;
}
try
{
using (SqlConnection con = new SqlConnection(ConnectionString))
{
con.Open();
// Verifica se email já existe
string checkQuery = "SELECT COUNT(*) FROM Pessoa WHERE Email = @Email";
using (SqlCommand checkCmd = new SqlCommand(checkQuery, con))
{
checkCmd.Parameters.Add("@Email", System.Data.SqlDbType.NVarChar, 100).Value = email;
int count = (int)checkCmd.ExecuteScalar();
if (count > 0)
{
MessageBox.Show("E-mail já cadastrado!");
return;
}
}
// Insere usuário
string insertQuery = "INSERT INTO Pessoa (Nome, Email, Senha) VALUES (@Nome, @Email, @Senha)";
using (SqlCommand cmd = new SqlCommand(insertQuery, con))
{
cmd.Parameters.Add("@Nome", System.Data.SqlDbType.NVarChar, 100).Value = nome;
cmd.Parameters.Add("@Email", System.Data.SqlDbType.NVarChar, 100).Value = email;
cmd.Parameters.Add("@Senha", System.Data.SqlDbType.NVarChar, 50).Value = senha;
cmd.ExecuteNonQuery();
MessageBox.Show("Cadastro realizado com sucesso!");
LimparCampos();
}
}
}
catch (Exception ex)
{
MessageBox.Show("Erro: " + ex.Message);
}
}
private void BtnLimpar_Click(object sender, EventArgs e)
{
LimparCampos();
Close();
}
private void LimparCampos()
{
txtNome.Text = "";
txtEmail.Text = "";
txtSenha.Text = "";
txtNome.Focus();
}
}
}