0% found this document useful (0 votes)
16 views

Text

This document contains C# code for a Windows Forms application that manages author data in a SQL database. It defines classes and methods to connect to the database, retrieve author data to display in a datagrid, add new authors, search for existing authors, and update author records. The form allows the user to view all authors, add new authors by entering a code and name, search for authors by code to edit records, and update author details in the database on button clicks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Text

This document contains C# code for a Windows Forms application that manages author data in a SQL database. It defines classes and methods to connect to the database, retrieve author data to display in a datagrid, add new authors, search for existing authors, and update author records. The form allows the user to view all authors, add new authors by entering a code and name, search for authors by code to edit records, and update author details in the database on button clicks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PROYECTO
{
public partial class Form1 : Form
{
SqlConnection cn = new SqlConnection("data source=DESKTOP-0C9G0LI\\
SQLEXPRESS; initial catalog=biblioteca; integrated security=true");

public DataTable listados_autores()


{ SqlCommand cmd = new SqlCommand("listado_autor", cn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter dat = new SqlDataAdapter(cmd);
DataTable tabla=new DataTable();
dat.Fill(tabla);
return tabla;
}
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
dataGridView1.DataSource = listados_autores();
}

private void button1_Click(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand("ingreso_autor", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@COD_AUTOR", SqlDbType.Char).Value = txt_ca.Text;
cmd.Parameters.Add("@AUTOR", SqlDbType.Char).Value = txt_autor.Text;
cn.Open();
cmd.ExecuteNonQuery();
dataGridView1.DataSource = listados_autores();
cn.Close();
}

private void txt_ca_Leave(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand("busqueda_autor", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@COD_AUTOR", SqlDbType.Char).Value = txt_ca.Text;
SqlDataAdapter dat = new SqlDataAdapter(cmd);
DataTable tabla = new DataTable();
dat.Fill(tabla);
if (tabla.Rows.Count > 0)
{
MessageBox.Show("El codigo del autor ya existe", "aviso");
txt_ca.Clear();
txt_ca.Focus();
}
else { txt_autor.Focus(); }
}

private void button2_Click(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand("busqueda_autor", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@COD_AUTOR", SqlDbType.Char).Value = txt_b.Text;
SqlDataAdapter dat = new SqlDataAdapter(cmd);
DataTable tabla = new DataTable();
dat.Fill(tabla);
if (tabla.Rows.Count > 0)
{
DataRow fila = tabla.Rows[0];
txt_ca.Text = fila[0].ToString();
txt_autor.Text = fila[1].ToString();

}
else { MessageBox.Show("El codigo del autor no existe", "aviso"); }
}

private void button3_Click(object sender, EventArgs e)


{
SqlCommand cmd = new SqlCommand("actualiza_autor", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@COD_AUTOR", SqlDbType.Char).Value = txt_ca.Text;
cmd.Parameters.Add("@AUTOR", SqlDbType.Char).Value = txt_autor.Text;
cn.Open();
cmd.ExecuteNonQuery();
dataGridView1.DataSource = listados_autores();
cn.Close();

private void groupBox1_Enter(object sender, EventArgs e)


{

}
}
}

You might also like