Vous êtes sur la page 1sur 32

[Escriba el ttulo del documento]

Universidad Nacional de Cajamarca


Facultad de Ingeniera
Escuela Acadmico Profesional de Ingeniera de Sistemas
CURSO : PROGRAMACIN APLICADA I

TEMA

MANUAL

INTEGRANTES:

ORRILLO VEGA, Roger Eduardo

DOCENTE :

Ing. CALOS APARACIO

CICLO

VI

MANUAL PROGRAMACIN APLICADA PARTE 2

1. Conectar Con SQL

Imports System.Data.SqlClient

2. Llenar un DataGridView

Imports System.Data.SqlClient Public Class WebForm1 Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim conex As New SqlConnection("server = localhost;database = Northwind; trusted_connection = true") 'puede ser ip, (local),nombre de BD, nombre servidor Dim datos As New SqlDataAdapter("select*from Estudiantesnew", conex) Dim ds As New Data.DataSet datos.Fill(ds, "Alumnos") GridView1.DataSource = ds.Tables("Alumnos") GridView1.DataBind()

3. Llamar un Procedimiento

If TextBox3.Text = "" Or TextBox4.Text = "" Then MsgBox("Faltan Datos") Else Dim insertar As New SqlCommand("Agr_Comp", conex) insertar.CommandType = CommandType.StoredProcedure insertar.Parameters.Add("@fec", SqlDbType.DateTime).Value =MaskedTextBox1.Text insertar.Parameters.Add("@prov", SqlDbType.VarChar, 20).Value = TextBox2.Text insertar.Parameters.Add("@total", SqlDbType.Float).Value = TextBox3.Text insertar.Parameters.Add("@boleta", SqlDbType.VarChar, 20).Value =TextBox4.Text conex.Open() resp = insertar.ExecuteNonQuery() conex.Close() If resp = 1 Then MessageBox.Show("se grabo") TextBox1.Focus() GroupBox2.Visible = True GroupBox1.Enabled = False End If End If

4. Uso de ExecuteNonQuery y executescalar()

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click Dim valor As Integer Dim consulta As New SqlCommand("select count(*) from usuario where idusuario=@id and password=@pas", conex) consulta.Parameters.Add("@id", SqlDbType.VarChar, 20).Value = UsernameTextBox.Text consulta.Parameters.Add("@pas", SqlDbType.VarChar, 20).Value = PasswordTextBox.Text conex.Open() valor = consulta.ExecuteScalar() conex.Close() If valor = 1 Then Form6.Show() Me.Hide() Else MessageBox.Show("NO existe Usuario o Contrasea esta mal") UsernameTextBox.Text = "" PasswordTextBox.Text = "" UsernameTextBox.Focus() End If

5. Llenar una tabla SQL con visual

Imports System.Data.SqlClient Public Class Form9 Dim resp As Integer Dim gen, estcv As Char Private Sub Form9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Sub limpiar_ingreso() TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" MaskedTextBox1.Text = "" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim insertar As New SqlCommand("insertar_pers", conex) insertar.CommandType = CommandType.StoredProcedure insertar.Parameters.Add("@n", SqlDbType.VarChar, 30).Value = TextBox2.Text insertar.Parameters.Add("@a", SqlDbType.VarChar, 40).Value = TextBox3.Text insertar.Parameters.Add("@e", SqlDbType.VarChar, 50).Value = TextBox4.Text If RadioButton1.Checked Then gen = "m" If RadioButton2.Checked Then gen = "f" insertar.Parameters.Add("@g", SqlDbType.Char, 1).Value = gen estcv = ListBox1.SelectedItem insertar.Parameters.Add("@ec", SqlDbType.Char, 1).Value = estcv insertar.Parameters.Add("@fn", SqlDbType.DateTime).Value = MaskedTextBox1.Text conex.Open() resp = insertar.ExecuteNonQuery() conex.Close() If resp = 1 Then MessageBox.Show("se grabo") limpiar_ingreso() End If Dim datos As New SqlClient.SqlDataAdapter("Select * from persona ", conex) Dim ds As New Data.DataSet datos.Fill(ds, "todos") DataGridView1.DataSource = ds.Tables("todos") 'muestra los datos de la tabla alumnos End Sub End Class

-Procedimiento almacenado

create proc insertar_pers @n varchar(30), @a varchar(40), @e varchar(50), @g char(1), @ec char(1), @fn datetime as insert into persona values (@n,@a,@e,@g,@ec,@fn)

6. Mantenimiento: Forma Automtica Seleccionar de dos maneras: datagridview o detalles:

Se arrastra al Formulario y listo

7. SQL Data Reader simple (un solo registro por columna)

Imports System.Data.SqlClient Public Class Form13 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim comando As New SqlCommand("select productname,unitprice,supplierid,unitsinstock from products where productid=@id", conex) comando.Parameters.Add("id", SqlDbType.Int).Value = CInt(TextBox1.Text) Dim regis As SqlDataReader conex.Open() regis = comando.ExecuteReader() regis.Read() If regis.HasRows Then TextBox2.Text = regis.Item(0) TextBox3.Text = regis.Item(1) TextBox4.Text = regis.Item(2) TextBox5.Text = regis.Item(3) Else MsgBox("no existe este codigo") End If regis.Close() conex.Close() End Sub

8. SQL Data Reader complejo (varios registros por columna)

Imports System.Data.SqlClient Public Class Form14 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim comando As New SqlCommand("select productname,unitprice,supplierid,unitsinstock from products where supplierid=@id", conex) comando.Parameters.Add("id", SqlDbType.Int).Value = CInt(TextBox1.Text) Dim regis As SqlDataReader conex.Open() regis = comando.ExecuteReader() Do While regis.Read()

MessageBox.Show(regis.Item(0) + " - " + CStr(regis.Item(1))) Loop regis.Close() conex.Close() End Sub Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus TextBox1.Text = "" End Sub End Class

9. Codigo para generar en un datagridview al hacer doble clic en otro DataGridView1.Rows(e.RowIndex).Cells(0).Value

REPORTES CONFIGURAR REPORTES Crear procedimiento en SQL Para ello se agrega un la herramienta ReportViewer1:

Se realiza de la siguiente manera:

EJERCICIOS EJERCICIO 1

Imports System.Data.SqlClient Public Class Form9 Dim resp As Integer Dim gen, estcv As Char Private Sub Form9_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Sub limpiar_ingreso() TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" MaskedTextBox1.Text = "" End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal System.EventArgs) Handles Button1.Click Dim insertar As New SqlCommand("insertar_pers", conex) insertar.CommandType = CommandType.StoredProcedure insertar.Parameters.Add("@n", SqlDbType.VarChar, 30).Value insertar.Parameters.Add("@a", SqlDbType.VarChar, 40).Value insertar.Parameters.Add("@e", SqlDbType.VarChar, 50).Value If RadioButton1.Checked Then gen = "m" If RadioButton2.Checked Then gen = "f" e As

= TextBox2.Text = TextBox3.Text = TextBox4.Text

insertar.Parameters.Add("@g", SqlDbType.Char, 1).Value = gen estcv = ListBox1.SelectedItem insertar.Parameters.Add("@ec", SqlDbType.Char, 1).Value = estcv insertar.Parameters.Add("@fn", SqlDbType.DateTime).Value = MaskedTextBox1.Text conex.Open() resp = insertar.ExecuteNonQuery() conex.Close() If resp = 1 Then MessageBox.Show("se grabo") limpiar_ingreso()

End If Dim datos As New SqlClient.SqlDataAdapter("Select * from persona ", conex) Dim ds As New Data.DataSet datos.Fill(ds, "todos") DataGridView1.DataSource = ds.Tables("todos") 'muestra los datos de la tabla alumnos End Sub End Class

EJERCICIO 2

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged Dim datos As New SqlClient.SqlDataAdapter("update productos set stock=stock+1 where cod_producto=153", conex) Dim ds As New Data.DataSet datos.Fill(ds, "Query4") DataGridView1.DataSource = ds.Tables("Query4") 'muestra los datos de la tabla alumnos End Sub

EJERCICIO 3 (CAMBIAR CONTRASEA)

Imports System.Data.SqlClient Public Class Form15 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If TextBox3.Text = TextBox4.Text Then Dim valor As Integer Dim query As New SqlCommand("select count(idusuario) from usuario where idusuario=@u and password=@p", conex) query.Parameters.Add("@u", SqlDbType.VarChar, 20).Value = TextBox1.Text query.Parameters.Add("@p", SqlDbType.VarChar, 20).Value = TextBox2.Text conex.Open() valor = query.ExecuteNONQUERY() 'cuando se usa execute reader se necesita guardar en algun lugar, con scalar no If valor = 1 Then Dim cambio As New SqlCommand("cpass", conex) cambio.CommandType = CommandType.StoredProcedure cambio.Parameters.Add("@np", SqlDbType.VarChar, 20).Value = TextBox4.Text cambio.Parameters.Add("@us", SqlDbType.VarChar, 20).Value = TextBox1.Text MsgBox("listo") Else MsgBox("Error, usuario-contrasea actual invalidos") End If Else MsgBox("claves no iguales") End If conex.Close() End Sub End Class

EJERCICIO 4 (DESARROLLO DE UN MENU CON IMAGENES)

EJERCICIO 5 (Mostrar las rdenes de ventas de una determinada fecha, obteniendo estas de un MonthCalendar.)

Imports System.Data.SqlClient Public Class Consulta12 Private Sub MonthCalendar1_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateChanged Dim data As New SqlDataAdapter("EJER31", conex) Dim ds As New Data.DataSet data.SelectCommand.CommandType = CommandType.StoredProcedure data.SelectCommand.Parameters.Add("@mda", SqlDbType.DateTime).Value = MonthCalendar1.SelectionStart data.Fill(ds, "EJER31") DataGridView1.DataSource = ds.Tables("EJER31") End Sub End Class

ALTER PROC [dbo].[EJER31] @mda as datetime AS SELECT O.OrderID, OrderDate, ProductName, P.UnitPrice, Quantity FROM Orders O INNER JOIN [Order Details] OD ON O.OrderID = OD.OrderID INNER JOIN Products P ON OD.ProductID = P.ProductID WHERE OrderDate = @mda

EJERCICIO 6 (actualizaciones constantes en un Datagriew)

EL cdigo SQl para el Datagriew ser:


create proc CONSULTAPERSONAS AS SELECT * FROM Personas

Imports System.Data.SqlClient Public Class Form21 Sub limpiar_ingreso() TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" TextBox2.Focus() End Sub Sub Cargardatos() Dim data As New SqlDataAdapter("CONSULTAPERSONAS", conex) Dim ds As New Data.DataSet data.SelectCommand.CommandType = CommandType.StoredProcedure data.Fill(ds, "CONSULTAPERSONA") DataGridView1.DataSource = ds.Tables("CONSULTAPERSONA") End Sub Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim resp As Integer Dim insert_per As New SqlCommand("INSERTAR_PERSONA", conex) insert_per.CommandType = CommandType.StoredProcedure insert_per.Parameters.Add("@N", SqlDbType.VarChar, 30).Value = TextBox2.Text insert_per.Parameters.Add("@APE", SqlDbType.VarChar, 40).Value = TextBox3.Text insert_per.Parameters.Add("@EM", SqlDbType.VarChar, 50).Value = TextBox4.Text If (RadioButton1.Checked = True) Then insert_per.Parameters.Add("@G", SqlDbType.Char, 1).Value = "M" ElseIf (RadioButton2.Checked = True) Then insert_per.Parameters.Add("@G", SqlDbType.Char, 1).Value = "F" End If insert_per.Parameters.Add("@EC", SqlDbType.Char, 1).Value = LTrim(ListBox1.SelectedItem) insert_per.Parameters.Add("@FN", SqlDbType.DateTime).Value = MaskedTextBox1.Text If (resp = 1) Then MessageBox.Show("Se grabo el registro") limpiar_ingreso() End If conex.Open() resp = insert_per.ExecuteNonQuery Cargardatos() End Sub

EJERCICIO 7 Crear un formulario con dos DataGridView, el primero debe contener CategoryID, CategoryName, Description , y al dar click en las celdas que contengan el CategoryID, se active el siguiente Datagriew que contendr ProductID,ProductName,UnitPrice, C.CategoryID

Consultas SQL create proc categorua as select CategoryID,CategoryName,Description from Categories create proc po @c int as select p.ProductID,p.ProductName,p.UnitPrice,c.CategoryID from Products p join Categories c on c.CategoryID=p.CategoryID where c.CategoryID=@c

pUblic Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim datos As New SqlDataAdapter("prod1", CONEX) Dim ds As New Data.DataSet datos.SelectCommand.CommandType = CommandType.StoredProcedure datos.Fill(ds, "prod1") DataGridView1.DataSource = ds.Tables(0) End Sub Private Sub DataGridView1_CellEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter Dim datos As New SqlDataAdapter("prod2", CONEX) Dim ds As New Data.DataSet

datos.SelectCommand.CommandType = CommandType.StoredProcedure datos.SelectCommand.Parameters.Add("@cat", SqlDbType.Int).Value = DataGridView1.Rows(e.RowIndex).Cells(0).Value 'Indica valor de filas y columnas seleccionada en el dtgv datos.Fill(ds, "prod2") DataGridView2.DataSource = ds.Tables(0) End Sub

De otra forma:
Imports System.Data.SqlClient Public Class Form1 Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load Dim data As New SqlDataAdapter("categorua", conex) Dim ds As New Data.DataSet data.SelectCommand.CommandType = CommandType.StoredProcedure data.Fill(ds, "CONSULTAPERSONA") DataGridView1.DataSource = ds.Tables("CONSULTAPERSONA") End Sub Private Sub DataGridView1_CellEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellEnter End Sub Private Sub DataGridView1_CellMouseClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick Dim insert_per As New SqlDataAdapter("po", conex) Dim ds As New Data.DataSet insert_per.SelectCommand.CommandType = CommandType.StoredProcedure insert_per.SelectCommand.Parameters.Add("@c", SqlDbType.Int).Value = DataGridView1.Rows(e.RowIndex).Cells(0).Value insert_per.Fill(ds, "CONSULTAPERSONA") DataGridView2.DataSource = ds.Tables("CONSULTAPERSONA") End Sub

EJERCICIO 8

Imports System.Data.SqlClient Public Class Form3 Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged If TextBox1.Text = "" Then Else Dim datos As New SqlClient.SqlDataAdapter("exec alumno " + CStr(TextBox1.Text), conex) Dim ds As New Data.DataSet datos.Fill(ds, "alumno") DataGridView1.DataSource = ds.Tables("alumno") End If End Sub Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TODO: esta lnea de cdigo carga datos en la tabla 'Semana_sistDataSet.CONFERENCIAS' Puede moverla o quitarla segn sea necesario. Me.CONFERENCIASTableAdapter.Fill(Me.Semana_sistDataSet.CONFERENCIAS) End Sub

Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress

If e.KeyChar = Convert.ToChar(Keys.Enter) Then ingresar(TextBox2.Text) End If mostrar() End Sub Sub mostrar() Dim datos As New SqlClient.SqlDataAdapter("exec ver_asistencia " + CStr(ListBox1.SelectedIndex), conex) Dim ds As New Data.DataSet datos.Fill(ds, "todos")

DataGridView2.DataSource = ds.Tables("todos") 'muestra los datos de la tabla alumnos End Sub

Sub ingresar(ByVal a) Dim resp As Integer Dim insertar As New SqlCommand("nva_asistencia", conex) insertar.CommandType = CommandType.StoredProcedure insertar.Parameters.Add("@FechaHorareg", SqlDbType.DateTime).Value = Now insertar.Parameters.Add("@IdParticipante", SqlDbType.Char, (10)).Value = a insertar.Parameters.Add("@IdConferencia", SqlDbType.Int).Value = ListBox1.SelectedIndex conex.Open() resp = insertar.ExecuteNonQuery() conex.Close() If resp = 1 Then MessageBox.Show("se grabo") TextBox2.Text = "" End If End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged mostrar() Label4.Text = ListBox1.Text End Sub

Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick ingresar(DataGridView1.Rows(e.RowIndex).Cells(0).Value) mostrar() End Sub End Class

EJERCICIO 9 ( uso de barra desplazamiento y gridview)

Comandos en SQL
ALTER PROCEDURE ORDENXEMPLE1 @EID INT AS SELECT E.EmployeeID, O.OrderID,OrderDate,C.CustomerID FROM Employees E INNER JOIN Orders O ON O.EmployeeID = E.EmployeeID INNER JOIN Customers C ON C.CustomerID = O.CustomerID WHERE E.EmployeeID = @EID ORDER BY E.EmployeeID

Visual Basic
Imports System.Data.SqlClient Public Class Form1 Private Sub EmployeesBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeesBindingNavigatorSaveItem.Click Me.Validate() Me.EmployeesBindingSource.EndEdit() Me.TableAdapterManager.UpdateAll(Me.NorthwindDataSet) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TODO: esta lnea de cdigo carga datos en la tabla 'NorthwindDataSet.Employees' Puede moverla o quitarla segn sea necesario. Me.EmployeesTableAdapter.Fill(Me.NorthwindDataSet.Employees) End Sub Sub CargarDatos() Dim conex As New SqlConnection("server = .; database = northwind; trusted_connection = true") Dim datos As New SqlDataAdapter("ORDENXEMPLE1", conex) Dim ds As New Data.DataSet datos.SelectCommand.CommandType = CommandType.StoredProcedure datos.SelectCommand.Parameters.Add("@eid", SqlDbType.Int).Value = EmployeeIDTextBox.Text datos.Fill(ds, "ORDENXEMPLE1") DataGridView1.DataSource = ds.Tables(0) End Sub Private Sub BindingNavigatorPositionItem_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles BindingNavigatorPositionItem.Enter CargarDatos() End Sub End Class

Ejercicios Finales
1) Mostrar en un detalle el Employee ID, Last Name, y el First Name de los empleado y anexa un Ddatagridview de tal manera que pueda visualizarle su ID, las rdenes de compra a las que est ligado, la fecha de la orden y el cdigo del cliente Establecemos el diseo de la consulta que se va a realizar:

Los comandos en SQL sern:

ALTER PROCEDURE ORDENXEMPLE1 @EID INT AS SELECT E.EmployeeID, O.OrderID,OrderDate,C.CustomerID FROM Employees E INNER JOIN Orders O ON O.EmployeeID = E.EmployeeID INNER JOIN Customers C ON C.CustomerID = O.CustomerID WHERE E.EmployeeID = @EID ORDER BY E.EmployeeID

La codificacin ser:
Imports System.Data.SqlClient Public Class Form1 Private Sub EmployeesBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeesBindingNavigatorSaveItem.Click Me.Validate() Me.EmployeesBindingSource.EndEdit() Me.TableAdapterManager.UpdateAll(Me.NorthwindDataSet) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TODO: esta lnea de cdigo carga datos en la tabla 'NorthwindDataSet.Employees' Puede moverla o quitarla segn sea necesario. Me.EmployeesTableAdapter.Fill(Me.NorthwindDataSet.Employees) End Sub Sub CargarDatos() Dim conex As New SqlConnection("server = .; database = northwind; trusted_connection = true") Dim datos As New SqlDataAdapter("ORDENXEMPLE1", conex) Dim ds As New Data.DataSet datos.SelectCommand.CommandType = CommandType.StoredProcedure datos.SelectCommand.Parameters.Add("@eid", SqlDbType.Int).Value = EmployeeIDTextBox.Text datos.Fill(ds, "ORDENXEMPLE1") DataGridView1.DataSource = ds.Tables(0) End Sub Private Sub BindingNavigatorPositionItem_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles BindingNavigatorPositionItem.Enter CargarDatos() End Sub End Class

Ejecutando programa

2) De un ListBox o ComboBox, escoger un ao y en un DatagridView, mostrar los clientes, el ao y el nmero de productos comprados:
El cdigo SQL ser:
CREATE PROC COMPRASANIO @ANIO NVARCHAR(4) AS SELECT C.CompanyName, YEAR(O.OrderDate) AS AO,SUM(QUANTITY) AS NUMPRODCOMPRADOS FROM Customers C INNER JOIN Orders O ON C.CustomerID = O.CustomerID INNER JOIN [Order Details] OD ON OD.OrderID=O.OrderID WHERE YEAR(O.OrderDate) = @ANIO GROUP BY YEAR (O.OrderDate), C.CompanyName,Quantity

El diseo ser:

La lnea de comandos en Visual Studio es:

Imports System.Data.SqlClient Public Class Form2 Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged Dim conex As New SqlConnection("server = .; database = northwind; trusted_connection = true") Dim datos As New SqlDataAdapter("COMPRASANIO ", conex) Dim ds As New Data.DataSet datos.SelectCommand.CommandType = CommandType.StoredProcedure datos.SelectCommand.Parameters.Add("@ANIO", SqlDbType.Int).Value = ComboBox1.SelectedItem datos.Fill(ds, "comprasanio") DataGridView1.DataSource = ds.Tables(0) End Sub End Class

En funcionamiento, el programa se ver as:

3) En un combo box, se tendr un alista de los clientes, de tal manera que al seleccionar a uno de ellos, un DataGridView, mostrar el nombre de la Compaa, OrderID, Ao de la Orden, Nombre Producto Comprado y su cantidad

La consulta SQL de bsqueda ser:


CREATE PROC BUSCARCLIENTE @CID CHAR(5) AS SELECT C.CompanyName,O.OrderID, YEAR(OrderDate), P.ProductName, Quantity FROM Customers C INNER JOIN Orders O ON C.CustomerID = O.CustomerID INNER JOIN [Order Details] OD ON O.OrderID=OD.OrderID INNER JOIN Products P ON P.ProductID=OD.ProductID WHERE C.CustomerID = @CID

VISTA:

Procedemos a configurar el ComboBox, mediante una conexin directa con la Base de Datos, seleccionando como miembro de muestra los nombres de los cliente y como miembros de valor ( bsqueda), el ID del cliente.

Para la configuracin del DataGridView:

Imports System.Data.SqlClient Public Class Form3 Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged Dim conex As New SqlConnection("server = .; database = northwind; trusted_connection = true") Dim datos As New SqlDataAdapter("BUSCARCLIENTE ", conex) Dim ds As New Data.DataSet datos.SelectCommand.CommandType = CommandType.StoredProcedure datos.SelectCommand.Parameters.Add("@CID", SqlDbType.Char, 5).Value = ComboBox1.SelectedValue datos.Fill(ds, "comprasanio") DataGridView1.DataSource = ds.Tables(0) End Sub Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TODO: esta lnea de cdigo carga datos en la tabla 'NorthwindDataSet1.Customers' Puede moverla o quitarla segn sea necesario. Me.CustomersTableAdapter.Fill(Me.NorthwindDataSet1.Customers) End Sub End Class

El funcionamiento del programa ser:

4) Realizar un Reporte que divido en aos y meses (matriz), que muestre el Monto Ingresado por las rdenes de compra. El Comando SQL es:
CREATE PROC VENTASANIOMES AS SELECT YEAR(O.OrderDate),MONTH(O.OrderDate), SUM(UnitPrice*Quantity) FROM Orders O INNER JOIN [Order Details] OD ON O.OrderID=OD.OrderID GROUP BY O.OrderDate, UnitPrice, Quantity

Public Class Form4 Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TODO: esta lnea de cdigo carga datos en la tabla 'NorthwindDataSet2.VENTASANIOMES' Puede moverla o quitarla segn sea necesario. Me.VENTASANIOMESTableAdapter.Fill(Me.NorthwindDataSet2.VENTASANIOMES) Me.ReportViewer1.RefreshReport() End Sub End Class

En funcionamiento el programa se ver:

5) Ver el nmero de productos por categora en un grfico La Consulta SQL ser:


CREATE PROC PRODCATEGO AS SELECT CategoryName, COUNT(*) FROM Categories C INNER JOIN Products P ON P.CATEGORYID = C.CategoryID GROUP BY C.CategoryName

El diseo del Reporte ser:

El programa en funcionamiento visualizar:

6) Mediante el objeto TabControl, mostrar en un DataGridView, el ProductID y el nombre del Producto, y cuando se haga click en un determinado ProductID, saldr otro DatagridView en el tab2 con un OrderId, UnitPrice la Cantidad, y el cdigo del Producto. Configuramos el diseo de los DataGridView, para cadauno de las consultas pedidas:

La programacin en Visual ser:


Imports System.Data.SqlClient Public Class Form6 Private Sub Form6_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'TODO: esta lnea de cdigo carga datos en la tabla 'NorthwindDataSet4.Products' Puede moverla o quitarla segn sea necesario. Me.ProductsTableAdapter.Fill(Me.NorthwindDataSet4.Products) End Sub Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick Dim conex As New SqlConnection("server = .; database = northwind; trusted_connection = true") Dim datos As New SqlDataAdapter("BUSCPROD", conex) Dim ds As New Data.DataSet datos.SelectCommand.CommandType = CommandType.StoredProcedure datos.SelectCommand.Parameters.Add("@PID", SqlDbType.Int).Value = DataGridView1.Rows(e.RowIndex).Cells(0).Value 'Indica valor de filas y columnas seleccionada en el dtgv datos.Fill(ds, "BUSCPROD") DataGridView2.DataSource = ds.Tables(0) End Sub End Class

Ahora, la vista del programa trabajando ser:

OTRAS HERRAMIENTAS Tool Strip Box: Permite la creacin de una barra de Mens con imgenes.

Para crear image list y ToolStrip Bottom:


Public Class Form1 Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) End Sub Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load ToolStrip1.ImageList = ImageList1 ToolStripButton1.ImageIndex = 0 ToolStripButton2.ImageIndex = 1 ToolStripButton3.ImageIndex = 2 ToolStripButton4.ImageIndex = 3 End Sub End Class

Menu Strip: Men normal como el de todos los programas.

Uso De Formulario Mdi: Sirve para que almacene a otros formularios en su interior Llamada De Un Formulario A Otro el formulario principal(padre) debe estar en mdi y luego se llamara con el cdigo: Form2.MdiParent = Me Form2.Show()

Navegador De Internet

Public Class Form3 Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load WebBrowser1.Navigate("http://www.google.com.pe/") End Sub Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted End Sub PARA AGREGAR UNA PAGINA MANUAL Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click WebBrowser1.Navigate(TextBox1.Text) End Sub End Class

Crear Un Modulo: un modulo se crea para almacenar rutinas que se comparten para varios formularios Module Module1 Public conex As New System.Data.SqlClient.SqlConnection("server=.;database = semana_sistemica;trusted_connection=true") End Module