Posted by taringa.net
Lo siguientes códigos son estructuras reconocibles pero a la hora de programar uno se olvida.
Lo mas importante, la conexión, conexion.cs
using System.data.sqlClient;
using System.Windows.Forms;
using System.data;
namespace examen
{
class conexion
{
SqlConnection con = new SqlConnection("Data Source=Nombre del servidor;Initial Catalog=Nombre de la bd;Integrated Security=true"![](https://o1.t26.net/images/big2v5.gif)
;
Si tiene seguridad con contraseña y usuario.
SqlConnection con = new SqlConnection(
"Data Source=Nombre del servidor;Initial Catalog=Nombre de la bd;User ID=usario;Password=contraseña"![](https://o1.t26.net/images/big2v5.gif)
;
private sqlcomandBuilder cmb;
public dataSet ds= new dataSet();
public sqldataAdapter da;
public SqlCommand comando;
public void Conexión()
{
try
{
con.Open();
MessageBox.show("conectado"![](https://o1.t26.net/images/big2v5.gif)
;
}
catch
{
MessageBox.show("no conectado"![](https://o1.t26.net/images/big2v5.gif)
;
}
finally
{
con.Close();
}
}
public void consulta ( string sql, string tabla)
{
ds.table.Clear();
da= new sqldataAdapter(sql, con);
cmb = new sqlcomandBuilder (da);
da.fill (ds, tabla);
}
public bool insertar (string sql)
{
con.Open=():
comando=new SqlCommand(sql, con);
int i= comando.ExecuteNonQuery();
con.Close();
if (i > 0)
{
return true;
}
else
{
return false;
}
}
public bool eliminar (string tabla, string condicion)
{
con.Open=():
string eliminar = "delete from "+ tabla+" where "+ condicion;
comando = new SqlCommand(elimina, con);
int i= comando.ExecuteNonQuery();
con.Close();
if (i > 0)
{
return true;
}
else
{
return false;
}
}
public bool actualizar (string tabla, string campos, string condicion)
{
con.Open=():
string actualizar = "update "+ tabla+" set "+ campos + " where " + condicion;
comando = new SqlCommand(actualizar, con);
int i= comando.ExecuteNonQuery();
con.Close();
if (i > 0)
{
return true;
}
else
{
return false;
}
}
}
}
---------------------------------------------------------------------------------------------
Seguimos con el form, creamos uno
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Conexion con = new Conexion();
private void Form1_Load(object sender, EventArgs e)
{
con.conectar(); //ejemplo
mostrarDatos();
//cmbSexo.SelectedIndex = 0;
}
private void btnAgregar_Click(object sender, EventArgs e)
{
string agregar = "insert into datos values (" + txtClave.Text + ",'" + txtNombre.Text + "','" + txtApellidoP.Text + "','" +
txtApellidoM.Text + "'," + txtEdad.Text + ",'" + cmbSexo.Text + "')";
if (con.insertar(agregar))
{
MessageBox.Show("Registro agregado correctamente"
![](https://o1.t26.net/images/big2v5.gif)
;
mostrarDatos();
}
else
{
MessageBox.Show("Error al agregar"![](https://o1.t26.net/images/big2v5.gif)
;
}
}
private void btnEliminar_Click(object sender, EventArgs e)
{
if (con.eliminar("datos","clave="+txtClave.text))
{
MessageBox.Show("Registro eliminado correctamente"![](https://o1.t26.net/images/big2v5.gif)
;
mostrarDatos();
}
else
{
MessageBox.Show("Error al eliminar"![](https://o1.t26.net/images/big2v5.gif)
;
}
}
private void btnModificar_Click(object sender, EventArgs e)
{
string actualizar = "update datos set edad = " + txtEdad.Text + " where clave = " + txtClave.Text;
if (con.actualizar("datos", actualizar, "clave=" + txtClave.text))
{
MessageBox.Show("Registro actualizado correctamente"![](https://o1.t26.net/images/big2v5.gif)
;
mostrarDatos();
}
else
{
MessageBox.Show("Error al actualizar"![](https://o1.t26.net/images/big2v5.gif)
;
}
}
private void btnBuscar_Click(object sender, EventArgs e)
{
dgvRegistros.DataSource = bd.SelectDataTable("select * from datos where clave = " + txtBuscar.Text);
}
private void dgvRegistros_CellContentClick(object sender, DataGridViewCellEventArgs e)
// o que sigue generar automáticamente en opciones, de esta manera cuando se hace click lo que esta dentro de la grilla se agregue al los texbox
{
DataGridViewRow dgv = dgvRegistros.Rows[e.RowIndex];
txtClave.Text = dgv.Cells[0].Value.ToString();
txtNombre.Text = dgv.Cells[1].Value.ToString();
txtApellidoP.Text = dgv.Cells[2].Value.ToString();
txtApellidoM.Text = dgv.Cells[3].Value.ToString();
txtEdad.Text = dgv.Cells[4].Value.ToString();
cmbSexo.Text = dgv.Cells[5].Value.ToString();
}
public void mostrarDatos()
{
con.consulta("select * from datos", "datos"![](https://o1.t26.net/images/big2v5.gif)
;
dgvRegistro.DataSource = con.ds.tables["datos"];
}
}
}
http://www.taringa.net/posts/info/19794967/C-SQL-server-Lo-que-te-olvidas.html?utm_medium=email&utm_source=taringa_mailing&utm_campaign=personalizados_DE_m_4&utm_content=9050&utm_term=post_1_button
Lo siguientes códigos son estructuras reconocibles pero a la hora de programar uno se olvida.
Lo mas importante, la conexión, conexion.cs
using System.data.sqlClient;
using System.Windows.Forms;
using System.data;
namespace examen
{
class conexion
{
SqlConnection con = new SqlConnection("Data Source=Nombre del servidor;Initial Catalog=Nombre de la bd;Integrated Security=true"
![](https://o1.t26.net/images/big2v5.gif)
![](https://o1.t26.net/images/space.gif)
Si tiene seguridad con contraseña y usuario.
SqlConnection con = new SqlConnection(
"Data Source=Nombre del servidor;Initial Catalog=Nombre de la bd;User ID=usario;Password=contraseña"
![](https://o1.t26.net/images/big2v5.gif)
![](https://o1.t26.net/images/space.gif)
private sqlcomandBuilder cmb;
public dataSet ds= new dataSet();
public sqldataAdapter da;
public SqlCommand comando;
public void Conexión()
{
try
{
con.Open();
MessageBox.show("conectado"
![](https://o1.t26.net/images/big2v5.gif)
![](https://o1.t26.net/images/space.gif)
}
catch
{
MessageBox.show("no conectado"
![](https://o1.t26.net/images/big2v5.gif)
![](https://o1.t26.net/images/space.gif)
}
finally
{
con.Close();
}
}
public void consulta ( string sql, string tabla)
{
ds.table.Clear();
da= new sqldataAdapter(sql, con);
cmb = new sqlcomandBuilder (da);
da.fill (ds, tabla);
}
public bool insertar (string sql)
{
con.Open=():
comando=new SqlCommand(sql, con);
int i= comando.ExecuteNonQuery();
con.Close();
if (i > 0)
{
return true;
}
else
{
return false;
}
}
public bool eliminar (string tabla, string condicion)
{
con.Open=():
string eliminar = "delete from "+ tabla+" where "+ condicion;
comando = new SqlCommand(elimina, con);
int i= comando.ExecuteNonQuery();
con.Close();
if (i > 0)
{
return true;
}
else
{
return false;
}
}
public bool actualizar (string tabla, string campos, string condicion)
{
con.Open=():
string actualizar = "update "+ tabla+" set "+ campos + " where " + condicion;
comando = new SqlCommand(actualizar, con);
int i= comando.ExecuteNonQuery();
con.Close();
if (i > 0)
{
return true;
}
else
{
return false;
}
}
}
}
---------------------------------------------------------------------------------------------
Seguimos con el form, creamos uno
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Conexion con = new Conexion();
private void Form1_Load(object sender, EventArgs e)
{
con.conectar(); //ejemplo
mostrarDatos();
//cmbSexo.SelectedIndex = 0;
}
private void btnAgregar_Click(object sender, EventArgs e)
{
string agregar = "insert into datos values (" + txtClave.Text + ",'" + txtNombre.Text + "','" + txtApellidoP.Text + "','" +
txtApellidoM.Text + "'," + txtEdad.Text + ",'" + cmbSexo.Text + "')";
if (con.insertar(agregar))
{
MessageBox.Show("Registro agregado correctamente"
![](https://o1.t26.net/images/big2v5.gif)
![](https://o1.t26.net/images/space.gif)
mostrarDatos();
}
else
{
MessageBox.Show("Error al agregar"
![](https://o1.t26.net/images/big2v5.gif)
![](https://o1.t26.net/images/space.gif)
}
}
private void btnEliminar_Click(object sender, EventArgs e)
{
if (con.eliminar("datos","clave="+txtClave.text))
{
MessageBox.Show("Registro eliminado correctamente"
![](https://o1.t26.net/images/big2v5.gif)
![](https://o1.t26.net/images/space.gif)
mostrarDatos();
}
else
{
MessageBox.Show("Error al eliminar"
![](https://o1.t26.net/images/big2v5.gif)
![](https://o1.t26.net/images/space.gif)
}
}
private void btnModificar_Click(object sender, EventArgs e)
{
string actualizar = "update datos set edad = " + txtEdad.Text + " where clave = " + txtClave.Text;
if (con.actualizar("datos", actualizar, "clave=" + txtClave.text))
{
MessageBox.Show("Registro actualizado correctamente"
![](https://o1.t26.net/images/big2v5.gif)
![](https://o1.t26.net/images/space.gif)
mostrarDatos();
}
else
{
MessageBox.Show("Error al actualizar"
![](https://o1.t26.net/images/big2v5.gif)
![](https://o1.t26.net/images/space.gif)
}
}
private void btnBuscar_Click(object sender, EventArgs e)
{
dgvRegistros.DataSource = bd.SelectDataTable("select * from datos where clave = " + txtBuscar.Text);
}
private void dgvRegistros_CellContentClick(object sender, DataGridViewCellEventArgs e)
// o que sigue generar automáticamente en opciones, de esta manera cuando se hace click lo que esta dentro de la grilla se agregue al los texbox
{
DataGridViewRow dgv = dgvRegistros.Rows[e.RowIndex];
txtClave.Text = dgv.Cells[0].Value.ToString();
txtNombre.Text = dgv.Cells[1].Value.ToString();
txtApellidoP.Text = dgv.Cells[2].Value.ToString();
txtApellidoM.Text = dgv.Cells[3].Value.ToString();
txtEdad.Text = dgv.Cells[4].Value.ToString();
cmbSexo.Text = dgv.Cells[5].Value.ToString();
}
public void mostrarDatos()
{
con.consulta("select * from datos", "datos"
![](https://o1.t26.net/images/big2v5.gif)
![](https://o1.t26.net/images/space.gif)
dgvRegistro.DataSource = con.ds.tables["datos"];
}
}
}
http://www.taringa.net/posts/info/19794967/C-SQL-server-Lo-que-te-olvidas.html?utm_medium=email&utm_source=taringa_mailing&utm_campaign=personalizados_DE_m_4&utm_content=9050&utm_term=post_1_button
Comentarios
Publicar un comentario