Clase 23-04-12-lado del cliente
- 1. MERINO ROMAN, Fiorella Pierina
Programando el Lado del Cliente
VENTAS.BOL
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Data;
usingSystem.Data.SqlClient;
using VENTAS.DAL;
namespace VENTAS.BOL
{
publicclassCategorias
{
privateint _IdCategoria;
privatestring _Categoria;
privatestring _Estado;
publicintIdCategoria
{
get
{
return _IdCategoria;
}
set
{
_IdCategoria = value;
}
}
publicstringCategoria
{
get
{
return _Categoria;
}
set
{
_Categoria = value;
}
}
publicstring Estado
{
get
{
return _Estado;
}
set
{
_Estado = value;
}
}
- 2. MERINO ROMAN, Fiorella Pierina
// Método para Insertar Registros
publicstaticintInsertar(Categorias c)
{
try
{
DBAccessdb = newDBAccess();
db.AddParameter("@Categoria",c.Categoria);
db.AddParameter("@Estado",c.Estado);
SqlParameter p = newSqlParameter("@IdCategoria",
SqlDbType.Int);
p.Direction = ParameterDirection.Output;
db.AddParameter(p);
intnRes = db.ExecuteNonQuery("Proc_Insertar_Categoria");
if (nRes == 1)
{
returnint.Parse(p.Value.ToString());
}
else
{
return -1;
}
}
catch (Exception e)
{
throw;
}
}
}
}
VENTAS.DAL
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Data;
usingSystem.Data.SqlClient;
namespace VENTAS.DAL
{
publicclassDBAccess : IDisposable
{
privateIDbCommandcmd = newSqlCommand();
privatestringstrConnectionString = "";
privateboolhandleErrors = false;
privatestringstrLastError = "";
publicDBAccess()
{
strConnectionString = "Data Source=.;Initial
Catalog=BD_Ventas;Integrated Security=True";
SqlConnectioncnx = newSqlConnection();
cnx.ConnectionString = strConnectionString;
cmd.Connection = cnx;
- 3. MERINO ROMAN, Fiorella Pierina
cmd.CommandType = CommandType.StoredProcedure;
}
publicIDataReaderExecuteReader()
{
IDataReader reader = null;
try
{
this.Open();
reader =
cmd.ExecuteReader(CommandBehavior.CloseConnection
);
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return reader;
}
publicIDataReaderExecuteReader(stringcommandtext)
{
IDataReader reader = null;
try
{
cmd.CommandText = commandtext;
reader = this.ExecuteReader();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return reader;
}
publicobjectExecuteScalar()
{
objectobj = null;
try
{
this.Open();
- 4. MERINO ROMAN, Fiorella Pierina
obj = cmd.ExecuteScalar();
this.Close();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
returnobj;
}
publicobjectExecuteScalar(stringcommandtext)
{
objectobj = null;
try
{
cmd.CommandText = commandtext;
obj = this.ExecuteScalar();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
returnobj;
}
publicintExecuteNonQuery()
{
int i = -1;
try
{
this.Open();
i = cmd.ExecuteNonQuery();
this.Close();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
- 5. MERINO ROMAN, Fiorella Pierina
catch
{
throw;
}
return i;
}
publicintExecuteNonQuery(stringcommandtext)
{
int i = -1;
try
{
cmd.CommandText = commandtext;
i = this.ExecuteNonQuery();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return i;
}
publicDataSetExecuteDataSet()
{
SqlDataAdapter da = null;
DataSet ds = null;
try
{
da = newSqlDataAdapter();
da.SelectCommand = (SqlCommand)cmd;
ds = newDataSet();
da.Fill(ds);
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return ds;
- 6. MERINO ROMAN, Fiorella Pierina
}
publicDataSetExecuteDataSet(stringcommandtext)
{
DataSet ds = null;
try
{
cmd.CommandText = commandtext;
ds = this.ExecuteDataSet();
}
catch (Exception ex)
{
if (handleErrors)
strLastError = ex.Message;
else
throw;
}
catch
{
throw;
}
return ds;
}
publicstringCommandText
{
get
{
returncmd.CommandText;
}
set
{
cmd.CommandText = value;
cmd.Parameters.Clear();
}
}
publicIDataParameterCollection Parameters
{
get
{
returncmd.Parameters;
}
}
publicvoidAddParameter(stringparamname, objectparamvalue)
{
SqlParameterparam = newSqlParameter(paramname,
paramvalue);
cmd.Parameters.Add(param);
}
publicvoidAddParameter(IDataParameterparam)
{
cmd.Parameters.Add(param);
- 7. MERINO ROMAN, Fiorella Pierina
}
publicstringConnectionString
{
get
{
returnstrConnectionString;
}
set
{
strConnectionString = value;
}
}
privatevoid Open()
{
cmd.Connection.Open();
}
privatevoid Close()
{
cmd.Connection.Close();
}
publicboolHandleExceptions
{
get
{
returnhandleErrors;
}
set
{
handleErrors = value;
}
}
publicstringLastError
{
get
{
returnstrLastError;
}
}
publicvoid Dispose()
{
cmd.Dispose();
}
}
}
- 8. MERINO ROMAN, Fiorella Pierina
VENTAS.UI
using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
using VENTAS.BOL;
namespace VENTAS4.UI
{
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}
privatevoid textBox1_TextChanged(object sender, EventArgs e)
{
}
privatevoid Form1_Load(object sender, EventArgs e)
{
}
privatevoid button1_Click(object sender, EventArgs e)
{
try
{
CategoriasobjCategoria = newCategorias();
objCategoria.Categoria = textBox2.Text;
objCategoria.Estado = "1";
textBox1.Text =
Categorias.Insertar(objCategoria).ToString();
MessageBox.Show("CategoríaInsertada", "Aviso",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex) {
MessageBox.Show(ex.Message , "Aviso", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}