You are on page 1of 4

CREATE PROCEDURE NOMBRE

<@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>,


AS
BEGIN
SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
END
GO
//CADENA DE CONECCION
public static SqlConnection ObtenerConexion()
{
SqlConnection Conn = new SqlConnection("Data Source=JONATHAN-PC;Initial Catalog=PlanEst;Integrated Security=True");
Conn.Open();
return Conn;
}

//AGREGAR A BASE DE DATOS


CREATE proc Insert_Doc
@NombreDoc varchar(50),
@Documento varbinary(MAX),
@FechaModificacion varchar(50)
as
insert into Documentos (NombreDocumento,Documento,FechaModificacion)
values(@NombreDoc,@Documento,@FechaModificacion)
GO
public static string Agregar(Ddocumentos pDocumento)
{
string rpta = "OK";
try
{
using (SqlConnection Conn = Conexion.ObtenerConexion())
{
SqlCommand Comando = new SqlCommand("Insert_Doc",Conn);
Comando.CommandType = CommandType.StoredProcedure;
Comando.Parameters.Add("@NombreDoc", SqlDbType.VarChar, 50).Value = pDocumento.NombreDoc;
Comando.Parameters.Add("@Documento", SqlDbType.VarBinary).Value = pDocumento.Documento;
Comando.Parameters.Add("@FechaModificacion", SqlDbType.VarChar, 100).Value = pDocumento.FechaModificacion;
Comando.ExecuteNonQuery();
Conn.Close();
}
}
catch (Exception ex)
{
rpta=ex.Message;
}
return rpta;
}

//ELIMINAR
CREATE proc Eliminar_Doc
@NombreDocumento varchar(50)
as
delete from Documentos
where NombreDocumento = @NombreDocumento
GO
public static string Eliminar(String NombreDoc)
{
string rpta = "OK";
try
{
using (SqlConnection conexion = Conexion.ObtenerConexion())
{
SqlCommand Comando = new SqlCommand("Eliminar_Doc", conexion);
Comando.CommandType = CommandType.StoredProcedure;
Comando.Parameters.Add("@NombreDocumento", SqlDbType.VarChar, 50).Value = NombreDoc;
Comando.ExecuteNonQuery();
conexion.Close();
}
}
catch (Exception ex)
{
rpta=ex.Message;
}
return rpta;
}

//MODIFICAR
CREATE proc [dbo].[Actualizar_documento]
@NombreDocumento varchar(50) ,
@Documento varbinary(MAX),
@FechaMod varchar(50)
as
update Documentos set Documento=@Documento, FechaModificacion=@FechaMod
where NombreDocumento = @NombreDocumento
GO
public static string Editar(Ddocumentos Doc)
{
string rpta = "OK";
try
{
using (SqlConnection conexion = Conexion.ObtenerConexion())
{
SqlCommand Comando = new SqlCommand("Actualizar_documento", conexion);
Comando.CommandType = CommandType.StoredProcedure;
Comando.Parameters.Add("@NombreDocumento", SqlDbType.VarChar, 50).Value = Doc.NombreDoc;
Comando.Parameters.Add("@Documento", SqlDbType.VarBinary).Value = Doc.Documento;
Comando.Parameters.Add("@FechaMod", SqlDbType.VarChar, 50).Value = Doc.FechaModificacion;
Comando.ExecuteNonQuery();
conexion.Close();
}
}
catch (Exception ex)
{
rpta = ex.Message;
}
return rpta;
}

//MOSTRAR
public DataTable Mostrar()
{
DataTable DtResultado = new DataTable("Documentos");
using (SqlConnection conexion = Conexion.ObtenerConexion())
{
SqlCommand commando = new SqlCommand(string.Format("select top 100 * from Documentos order by
NombreDocumento asc "), conexion);
SqlDataAdapter SqlDat = new SqlDataAdapter(commando);
SqlDat.Fill(DtResultado);
conexion.Close();
return DtResultado;
}
}

//BUSCAR
CREATE proc [dbo].[buscar_Doc_Nombre]
@NombreDocumento varchar(50)
as
select * from Documentos
where NombreDocumento = @NombreDocumento
GO
public DataTable Buscar( String NombreDoc )
{
DataTable DtResultado = new DataTable("Docentes");
try
{
using (SqlConnection conexion = Conexion.ObtenerConexion())
{
SqlCommand Comando = new SqlCommand("buscar_Doc_Nombre", conexion);
Comando.CommandType = CommandType.StoredProcedure;
Comando.Parameters.Add("@NombreDocumento", SqlDbType.VarChar, 50).Value = NombreDoc;
Comando.ExecuteNonQuery();
SqlDataAdapter SqlDat = new SqlDataAdapter(Comando);
SqlDat.Fill(DtResultado);
conexion.Close();
}
}
catch (Exception ex)
{
DtResultado=null;
}
return DtResultado;
}

METODO SIN PROCEDIMIENTOS ALMACENADOS


class conexion
{
public static SqlConnection ObtenerConexion()
{
SqlConnection Conn = new SqlConnection("Data Source=(local);Initial Catalog=SistemaSeguridad;Integrated
Security=True");
Conn.Open();
return Conn;
}
}

//MOSTRAR
void MostrarDatos()
{
DataTable DtResultado = new DataTable("usuarios");
using (SqlConnection con = conexion.ObtenerConexion())
{
SqlCommand commando = new SqlCommand(string.Format("select top 100 * from Usuario order by Usuario asc "), con);
SqlDataAdapter SqlDat = new SqlDataAdapter(commando);
SqlDat.Fill(DtResultado);
con.Close();
}
this.listado.DataSource = DtResultado;
this.listado.Columns[0].Visible = false;
listado.RowsDefaultCellStyle.BackColor = Color.LightBlue;
listado.AlternatingRowsDefaultCellStyle.BackColor = Color.White;
}

//MODIFICAR
private void Modificar_Click(object sender, EventArgs e)
{
int retorno = 0;
string rpta = "";
try
{
using (SqlConnection con = conexion.ObtenerConexion())
{
SqlCommand Comando = new SqlCommand("Modificar_Usuario", con);
Comando.CommandType = CommandType.StoredProcedure;
Comando.Parameters.Add("@Id", SqlDbType.Int).Value = Id;
Comando.Parameters.Add("@Usuario", SqlDbType.VarChar, 50).Value = txtusuario.Text;
Comando.Parameters.Add("@Contrasea", SqlDbType.VarChar, 50).Value = txtcontrasea.Text;
Comando.Parameters.Add("@Acceso", SqlDbType.VarChar, 50).Value = txtacceso.Text;
retorno = Comando.ExecuteNonQuery();
con.Close();
}
}
catch (Exception ex)
{
rpta = ex.Message;
}
if (retorno > 0)
{
MessageBox.Show("Datos Modificados correctamente", "Datos Modificados", MessageBoxButtons.OK,
MessageBoxIcon.Information);
Limpiar();
MostrarDatos();
HabitaElim_Mod(false);
}
else
{
MessageBox.Show(rpta, "Error al Modificados", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}

//ELIMINAR
private void Eliminar_Click(object sender, EventArgs e)
{
int resultado = 0;
using (SqlConnection con = conexion.ObtenerConexion())
{
SqlCommand comando = new SqlCommand(string.Format("Delete from Usuario where Id={0}", Id), con);
resultado = comando.ExecuteNonQuery();
con.Close();
}
if (resultado > 0)
{
MessageBox.Show("Datos Eliminados correctamente", "Datos eliminar", MessageBoxButtons.OK, MessageBoxIcon.Information);
Limpiar();
MostrarDatos();
HabitaElim_Mod(false);
}
else
{MessageBox.Show("No se pudieron eliminar los datos", "Error al eliminar", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
}

//AGREGAR
private void Guardar_Click(object sender, EventArgs e)
{
if (txtusuario.Text == "")
{
MessageBox.Show("Digite Usuario", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
txtusuario.Focus();
}
else
{
if (txtcontrasea.Text == "")
{
MessageBox.Show("Digite la contrasea", "Advertencia", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
txtcontrasea.Focus();
}
else
{
if (txtacceso.Text == "")
{
MessageBox.Show("Digite el acceso", "Advertencia", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
txtcontrasea.Focus();
}
else
{
int resultado = 0;
using (SqlConnection Conn = conexion.ObtenerConexion())
{
SqlCommand Comando = new SqlCommand(string.Format(
"Insert Into Usuario( Usuario,Contrasea, Acceso) values ('{0}', '{1}', '{2}' )",
txtusuario.Text, txtcontrasea.Text, txtacceso.Text), Conn);
resultado = Comando.ExecuteNonQuery();
}
MostrarDatos();
if (resultado > 0)
{
MessageBox.Show("Datos Guardados correctamente", "Datos guardados", MessageBoxButtons.OK, MessageBoxIcon.Information);
Limpiar();
}
else
{ MessageBox.Show("No se pudieron guardar los datos", "Error al guardar", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
}
}
}
}

You might also like