public int AddTenant(Tenant objTenant) { string procName = "SP_TenantInsert"; var param = new DynamicParameters(); int tenantId = 0; param.Add("@TenantId", objTenant.TenantId, null, ParameterDirection.Output); param.Add("@FirstName", objTenant.FirstName); param.Add("@MiddleName", objTenant.MiddleName); param.Add("@LastName", objTenant.LastName); param.Add("@ContactNumber1", objTenant.ContactNumber1); param.Add("@ContactNumber2", objTenant.ContactNumber2); try { SqlMapper.Execute(_objConnection.GetConnection, procName, param, commandType: CommandType.StoredProcedure); tenantId = param.Get <int>("@TenantId"); } finally { _objConnection.CloseConnection(); } return(tenantId); }
private void Form1_Load(object sender, EventArgs e) { SqlConnection connection = null; try { connection = ConnectionFactory.GetConnection(); SqlCommand command = new SqlCommand(); command.Connection = connection; command.CommandType = CommandType.Text; command.CommandText = "select * from cliente"; SqlDataReader dataReader = command.ExecuteReader(); List <Cliente> clientes = new List <Cliente>(); while (dataReader.Read()) { Cliente cliente = new Cliente(); cliente.Id = dataReader.GetInt32(0); cliente.Nome = dataReader.GetString(1); cliente.Telefone = dataReader.GetString(2); clientes.Add(cliente); } dtgClientes.DataSource = clientes; } catch (Exception ex) { MessageBox.Show("Erro: " + ex.Message); } finally { ConnectionFactory.CloseConnection(connection); } }
private void btnSalvar_Click(object sender, EventArgs e) { String nome = txtNome.Text; String telefone = txtTelefone.Text; SqlConnection connection = null; try { connection = ConnectionFactory.GetConnection(); SqlCommand command = new SqlCommand(); command.Connection = connection; command.CommandType = CommandType.Text; command.CommandText = "insert into Cliente (Nome, Telefone) values ('" + nome + "', '" + telefone + "')"; int n = command.ExecuteNonQuery(); } catch (Exception ex) { MessageBox.Show("Erro: " + ex.Message); } finally { txtNome.Text = ""; txtTelefone.Text = ""; ConnectionFactory.CloseConnection(connection); } }
protected bool TableExists() { if (string.IsNullOrEmpty(tableName)) { throw new InvalidOperationException("Tablename not specified"); } bool tableChecked = tablesChecked.Any(t => t.Equals(tableName)); if (tableChecked) { return(true); } try { string sql = $"select count(*) from {tableName}"; ConnectionFactory.OpenConnection(); ConnectionFactory.CreateCommand(sql); ConnectionFactory.ExecuteCommand(); ConnectionFactory.CloseConnection(); tablesChecked.Add(tableName); return(true); } catch (Exception ex) { ConnectionFactory.CloseConnection(); return(false); } }
private void btnSalvar_Click(object sender, EventArgs e) { String nome = txtNome.Text; String tel = txtTelefone.Text; SqlConnection connection = null; try { connection = ConnectionFactory.GetConnection(); SqlCommand command = new SqlCommand(); command.Connection = connection; command.CommandType = CommandType.Text; command.CommandText = "insert into cliente (nome, telefone) values (@nome, @telefone)"; command.Parameters.AddWithValue("nome", nome); command.Parameters.AddWithValue("telefone", tel); int n = command.ExecuteNonQuery(); MessageBox.Show("Usuário inserido na base!"); clearFields(); } catch (Exception ex) { MessageBox.Show("Erro: " + ex.Message); } finally { ConnectionFactory.CloseConnection(connection); } }
private void txtProcurar_TextChanged(object sender, EventArgs e) { String nome = txtProcurar.Text; SqlConnection connection = null; try { connection = ConnectionFactory.GetConnection(); SqlCommand command = new SqlCommand(); command.Connection = connection; command.CommandType = CommandType.Text; // SELECT * FROM cliente WHERE nome LIKE '%L%'; command.CommandText = "SELECT * FROM cliente WHERE nome = @nome"; command.Parameters.AddWithValue("nome", nome); SqlDataReader reader = command.ExecuteReader(); List <Cliente> clientes = new List <Cliente>(); while (reader.Read()) { Cliente cliente = new Cliente(); cliente.Id = reader.GetInt32(0); cliente.Nome = reader.GetString(1); cliente.Telefone = reader.GetString(2); clientes.Add(cliente); } dataGridView1.DataSource = clientes; } catch (Exception ex) { MessageBox.Show("Erro: " + ex.Message); } finally { ConnectionFactory.CloseConnection(connection); } }
public void CloseConnection() { connFactory.CloseConnection(); }