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

Sqlconnection Sqlconnection Dataset Datatable

The document contains C# code for connecting to a SQL database and performing CRUD operations on an "Areas" table. It defines methods to retrieve, insert, update, and delete data from the Areas table. It also includes code for a web form page that allows inserting new area records.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

Sqlconnection Sqlconnection Dataset Datatable

The document contains C# code for connecting to a SQL database and performing CRUD operations on an "Areas" table. It defines methods to retrieve, insert, update, and delete data from the Areas table. It also includes code for a web form page that allows inserting new area records.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

private SqlConnection con = new SqlConnection("Data Source=HP-PC\\SQLEXPRESS;Initial

Catalog=laboratorio16;Integrated Security=True");
private DataSet ds;

public DataTable MostrarDatos()


{

con.Open();
SqlCommand cmd = new SqlCommand("select*from Areas", con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ds = new DataSet();
ad.Fill(ds, "table");
con.Close();
return ds.Tables["Tabla"];

}
public DataTable Buscar(string nombre)
{

con.Open();
SqlCommand cmd = new SqlCommand(string.Format("select*from Areas where
areNombre like '%{0}%'", nombre), con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ds = new DataSet();
ad.Fill(ds, "table");
con.Close();
return ds.Tables["Tabla"];
}

public bool Insertar(string arecodigo, string arenombre, string aretiempo) {

con.Open();
SqlCommand cmd = new SqlCommand(string.Format("insert into Areas values
{0}, '{1}', '{2}'", new string[] {arecodigo, arenombre, aretiempo}));
int filasafectadas = cmd.ExecuteNonQuery();
if (filasafectadas > 0) return true;
else return false;
}
public bool Eliminar(string arecodigo)
{

con.Open();
SqlCommand cmd = new SqlCommand(string.Format("delete from Areas where
arecodigo= {0}", arecodigo),con);
int filasafectadas = cmd.ExecuteNonQuery();
if (filasafectadas > 0) return true;
else return false;
}

public bool Actualizar(string arecodigo, string arenombre, string aretiempo)


{

con.Open();
SqlCommand cmd = new SqlCommand(string.Format("update Areas set
areNombre={0}, areTiempo={1}, where areCodigo= {2}", new string[](arecodigo,
arenombre, aretiempo)), con);
int filasafectadas = cmd.ExecuteNonQuery();
if (filasafectadas > 0) return true;
else return false;
}

///////////////

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace Laboratorio16
{
public partial class Ingresoarea : System.Web.UI.Page
{

SqlConnection con = new SqlConnection("Data Source=HP-PC\\SQLEXPRESS;Initial


Catalog=laboratorio16;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
{

protected void btnguardar_Click(object sender, EventArgs e)


{

try
{
if (String.IsNullOrEmpty(txtnomarea.Text) ||
String.IsNullOrEmpty(txttiemarea.Text))
{
Label4.Text = ("Ingrese un Dato");

}
else
{

con.Open();
SqlCommand insertar = new SqlCommand("insert into Areas
values(@cod, @nom, @tiem)", con);
insertar.Parameters.AddWithValue("@cod", txtcodarea.Text);
insertar.Parameters.AddWithValue("@nom", txtnomarea.Text);
insertar.Parameters.AddWithValue("@tiem", txttiemarea.Text);
insertar.ExecuteNonQuery();
txtnomarea.Text = "";
txttiemarea.Text = "";
txtcodarea.Text = "";
con.Close();

Label4.Text = ("Dato agregado con Éxito");

}
}
catch {
Label4.Text = ("Se ha generado una excepción");

protected void dgv_SelectedIndexChanged(object sender, EventArgs e)


{

}
}

Clsconexion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Laboratorio16
{
public class clsconexion
{

protected SqlConnection oconeccion = new SqlConnection();


string cadena = "Data Source =HP-PC\\SQLEXPRESS; initial
Catalog=laboratorio16; Integrated Security=true";
public SqlConnection conectarbd = new SqlConnection();

public clsconexion()
{
conectarbd.ConnectionString = cadena;
}

public void abrir()


{
try
{
conectarbd.Open();
Console.WriteLine("conexion abierta");

}
catch (Exception ex) {

Console.WriteLine("Error al abrir la BD "+ ex.Message);

}
}

public void cerrar() {

conectarbd.Close();
}
}
}

//////////////////////

namespace Laboratorio16
{
public class clssconexion
{
protected SqlDataReader reader;
protected SqlDataAdapter AdaptadorDatos;
protected DataSet data;
protected SqlConnection oconeccion= new SqlConnection();

public clssconexion(){

public void conectar(string tabla)


{
string strConeccion =
ConfigurationManager.ConnectionStrings["laboratorio16ConnectionString"].ConnectionSt
ring;
oconeccion.ConnectionString = strConeccion;
AdaptadorDatos= new SqlDataAdapter("select* from"+ tabla, oconeccion);
SqlCommandBuilder ejecutacomandos = new
SqlCommandBuilder(AdaptadorDatos);
Data = new DataSet();
AdaptadorDatos.Fill(Data, tabla);
oconeccion.Close();

public DataSet Data{


set{data= value;}
get{ return data;}
}

public SqlDataReader DataReader{


set{ reader= value;}
get{ return reader;}
}
}
}

You might also like