Está en la página 1de 2

MySqlConnection Ocon = new MySqlConnection();

public BaseDatos()
{
this.Connect();
}
public void Connect()
{

SqlConnection Ocon = new SqlConnection("Data Source=USUARIO-


PC\\SQLEXPRESS;Initial Catalog=bdNaturvida;Integrated Security=True");
Ocon.Open();

/**
if(Ocon.State == ConnectionState.Closed)
{
Ocon.ConnectionString = String.Format("Data Source=USUARIO-
PC\\SQLEXPRESS;Initial Catalog=bdNaturvida;Integrated Security=True");
Ocon.Open();
}
* **/

//Insert, Update, Delete


public int Query(String sql)
{
Ocon.Open();

MySqlCommand command = new MySqlCommand(sql, Ocon);


return command.ExecuteNonQuery();
}

//Select
public DataTable getData(string sql)
{
this.Connect();
DataTable table = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter(sql, Ocon);
adapter.Fill(table);
return table;
}

//Obtener una fila de la tabla retornada por getData


public DataRow getRow(string sql)
{
DataRow row = null;
if (this.getData(sql).Rows.Count == 0)
{
return null;
}
row = this.getData(sql).Rows[0];
return row;
}

//Metodo para cargar comboBox


public void CargarCombo(ComboBox cbo, String sql, String mostrar, String
seleccionar)
{
this.Connect();
DataTable datos = this.getData(sql);

if (datos.Rows.Count > 0)
{
cbo.DataSource = null;
cbo.DataSource = datos;
cbo.DisplayMember = mostrar;
cbo.ValueMember = seleccionar;
}
else
{
cbo.Text = "No hay registros";
cbo.SelectedIndex = -1;
}

También podría gustarte