public void atualizarTrans(CadastroTrans a) { if (conn.State == ConnectionState.Open) { SqlCommand cmd = new SqlCommand("UPDATE Transacao SET Valor=@1, DataTrans=@2, Origem=@3, TipoMoeda=@4, Destino=@5 where idTransacao=@0", conn); cmd.Parameters.AddWithValue("@1", a.getValor()); cmd.Parameters.Add("@2", SqlDbType.Date, 90).Value = a.getdataTrans(); cmd.Parameters.AddWithValue("@3", a.getidOrigem()); cmd.Parameters.AddWithValue("@4", a.getidMoeda()); cmd.Parameters.AddWithValue("@5", a.getidDestino()); cmd.Parameters.AddWithValue("@0", a.getId()); cmd.CommandType = CommandType.Text; //conn.Open(); try { int i = cmd.ExecuteNonQuery(); if (i > 0) { MessageBox.Show("Registro atualizado com sucesso!"); } } catch (Exception ex) { MessageBox.Show("Erro: " + ex.ToString()); } finally { conn.Close(); } } }
public void listarTable() { for (int i = 0; i < grdUsu.RowCount; i++) { grdUsu.Rows[i].DataGridView.Rows.Clear(); } // Pega os Id da tabela do banco de dados int count = TransacaoDAO.quantidade(); int aa = 0; for (int i = 0; i <= count; i++) { if (!(TransacaoDAO.listarUsu(i) == null)) { CadastroTrans a = TransacaoDAO.listarUsu(i); grdUsu.Rows.Add(); grdUsu.Rows[aa].Cells[0].Value = a.getId(); grdUsu.Rows[aa].Cells[1].Value = a.getValor(); grdUsu.Rows[aa].Cells[2].Value = a.getdataTrans(); grdUsu.Rows[aa].Cells[3].Value = a.getOrigem(); grdUsu.Rows[aa].Cells[4].Value = a.getMoeda(); grdUsu.Rows[aa].Cells[5].Value = a.getDestino(); aa++; } } listarMoedas(); listarOrigem(); listarDestino(); }
public CadastroTrans getIds(CadastroTrans a) { conn = classeConexão.obterConexao(); if (conn.State == ConnectionState.Open) { string aux = null; SqlCommand cmd1; SqlDataReader reader; conn = classeConexão.obterConexao(); if (a.getOrigem() != "") { cmd1 = new SqlCommand("SELECT idCarteira FROM Usuario WHERE Nome=@1", conn); cmd1.Parameters.Add("@1", SqlDbType.NVarChar, 90).Value = a.getOrigem(); reader = cmd1.ExecuteReader(); if (reader.Read()) { aux = (reader[0].ToString()); } cmd1.Dispose(); reader.Close(); a.setidOrigem(int.Parse(aux)); } cmd1 = new SqlCommand("SELECT idMoeda FROM Moeda WHERE NomeMoeda=@1", conn); cmd1.Parameters.Add("@1", SqlDbType.NVarChar, 90).Value = a.getMoeda(); reader = cmd1.ExecuteReader(); if (reader.Read()) { aux = (reader[0].ToString()); } cmd1.Dispose(); reader.Close(); a.setidMoeda(int.Parse(aux)); cmd1 = new SqlCommand("SELECT idCarteira FROM Usuario WHERE Nome=@1", conn); cmd1.Parameters.Add("@1", SqlDbType.NVarChar, 90).Value = a.getDestino(); reader = cmd1.ExecuteReader(); if (reader.Read()) { aux = (reader[0].ToString()); } cmd1.Dispose(); reader.Close(); a.setidDestino(int.Parse(aux)); return(a); } else { return(null); } }
private void btnEditar_Click(object sender, EventArgs e) { CadastroTrans a = new CadastroTrans(); a.setId(int.Parse(txtId.Text)); a.setdataTrans(DateTime.Parse(txtData.Text)); a.setDestino(cbDestino.SelectedItem.ToString()); a.setMoeda(cbMoeda.SelectedItem.ToString()); a.setOrigem(cbOrigem.SelectedItem.ToString()); a.setValor(Double.Parse(txtValord.Text)); TransacaoDAO.atualizarTrans(a); listarTable(); txtId.Clear(); txtData.Clear(); txtValord.Clear(); }
public void criarTrans(CadastroTrans cad) { //conn = new SqlConnection(connString); string aux = null; SqlCommand cmd1; SqlDataReader reader; if (conn.State == ConnectionState.Open) { try { string queryString = "INSERT INTO Transacao (Valor,DataTrans,Origem,TipoMoeda,Destino) VALUES (@1,@2,@3,@4,@5)"; SqlCommand cmd = new SqlCommand(queryString, conn); cmd.Parameters.Add("@1", SqlDbType.NVarChar, 90).Value = cad.getValor(); cmd.Parameters.Add("@2", SqlDbType.Date, 90).Value = cad.getdataTrans(); if (cad.getOrigem() != "") { cmd1 = new SqlCommand("SELECT idCarteira FROM Usuario WHERE Nome=@1", conn1); cmd1.Parameters.Add("@1", SqlDbType.NVarChar, 90).Value = cad.getOrigem(); reader = cmd1.ExecuteReader(); if (reader.Read()) { aux = (reader[0].ToString()); } cmd1.Dispose(); reader.Close(); cmd.Parameters.Add("@3", SqlDbType.NVarChar, 90).Value = int.Parse(aux); } else { cmd.Parameters.Add("@3", SqlDbType.NVarChar, 90).Value = null; } cmd1 = new SqlCommand("SELECT idMoeda FROM Moeda WHERE NomeMoeda=@1", conn1); cmd1.Parameters.Add("@1", SqlDbType.NVarChar, 90).Value = cad.getMoeda(); reader = cmd1.ExecuteReader(); if (reader.Read()) { aux = (reader[0].ToString()); } cmd1.Dispose(); reader.Close(); cmd.Parameters.Add("@4", SqlDbType.NVarChar, 90).Value = int.Parse(aux); cmd1 = new SqlCommand("SELECT idCarteira FROM Usuario WHERE Nome=@1", conn1); cmd1.Parameters.Add("@1", SqlDbType.NVarChar, 90).Value = cad.getDestino(); reader = cmd1.ExecuteReader(); if (reader.Read()) { aux = (reader[0].ToString()); } cmd1.Dispose(); reader.Close(); cmd.Parameters.Add("@5", SqlDbType.NVarChar, 90).Value = int.Parse(aux); cmd.ExecuteScalar(); MessageBox.Show("Registro inserido com sucesso!"); } catch (Exception error) { MessageBox.Show("Erro: " + error); } finally { classeConexão.fecharConexao(); } } }
public CadastroTrans listarUsu(int id) { if (conn.State == ConnectionState.Open) { SqlCommand cmd = new SqlCommand("select * from Transacao where idTransacao = @id", conn); cmd.Parameters.AddWithValue("@id", id); try { CadastroTrans a = new CadastroTrans(); SqlDataReader reader = cmd.ExecuteReader(); if (reader.Read()) { a.setId(int.Parse(reader[0].ToString())); a.setValor(double.Parse(reader[1].ToString())); a.setdataTrans(DateTime.Parse(reader[2].ToString())); SqlCommand cmd1 = new SqlCommand("SELECT Nome FROM Usuario WHERE idCarteira=@1", conn1); cmd1.Parameters.AddWithValue("@1", reader[3]); SqlDataReader reader1 = cmd.ExecuteReader(); if (reader1.Read()) { a.setOrigem(reader[0].ToString()); } cmd1.Dispose(); reader1.Close(); cmd1 = new SqlCommand("SELECT NomeMoeda FROM Moeda WHERE idMoeda=@1", conn1); cmd1.Parameters.AddWithValue("@1", reader[4]); reader1 = cmd.ExecuteReader(); if (reader1.Read()) { a.setMoeda(reader[0].ToString()); } cmd1.Dispose(); reader1.Close(); cmd1 = new SqlCommand("SELECT Nome FROM Usuario WHERE idCarteira=@1", conn1); cmd1.Parameters.AddWithValue("@1", reader[5]); reader1 = cmd.ExecuteReader(); if (reader1.Read()) { a.setDestino(reader[0].ToString()); } cmd1.Dispose(); reader1.Close(); return(a); } else { return(null); } } catch (Exception error) { MessageBox.Show("Erro: " + error); return(null); } finally { classeConexão.fecharConexao(); } } else { return(null); } }