-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data_Base.cs
95 lines (81 loc) · 2.77 KB
/
Data_Base.cs
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
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Inscricao_e_Matricula
{
public class Data_Base
{
static string data_source = "datasource=localhost; username=root; password=mariopaulos06; database=insc_mat";
public static MySqlConnection conexao;
public static MySqlConnection Conexao_Banco()
{
conexao = new MySqlConnection(data_source);
conexao.Open();
return conexao;
}
public DataTable Obter_Usuarios()
{
MySqlDataAdapter data = null;
DataTable dataTable = new DataTable();
try
{
using (var cmd = Conexao_Banco().CreateCommand())
{
cmd.CommandText = "SELECT * FROM usuarios";
data = new MySqlDataAdapter(cmd.CommandText, Conexao_Banco());
data.Fill(dataTable);
Conexao_Banco().Close();
return dataTable;
}
}
catch (Exception ex)
{
throw ex;
}
}
public static DataTable Consulta(string mySql)
{
MySqlDataAdapter data = null;
DataTable dataTable = new DataTable();
try
{
using (var cmd = Conexao_Banco().CreateCommand())
{
cmd.CommandText = mySql;
data = new MySqlDataAdapter(cmd.CommandText, Conexao_Banco());
data.Fill(dataTable);
Conexao_Banco().Close();
return dataTable;
}
}
catch (Exception ex)
{
Conexao_Banco().Close();
throw ex;
}
}
public static void Nova_Entidade(Entidade e)
{
try
{
var cmd = Conexao_Banco().CreateCommand();
cmd.CommandText = "INSERT INTO usuario (nome_usuario,senha_usuario,entidade_usuario,id_usuario) VALUES(@nome, @senha, @entidade,(SELECT MAX(id_usuario)+1 FROM usuario))";
cmd.Parameters.AddWithValue("@nome", e.nome_entidade);
cmd.Parameters.AddWithValue("@senha", e.senha_entidade);
cmd.Parameters.AddWithValue("@entidade", e.tipo_entidade);
MessageBox.Show("Entidade Criada!");
Conexao_Banco().Close();
}
catch (Exception ex)
{
Conexao_Banco().Close();
MessageBox.Show(" " + ex.Message);
}
}
}
}