elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Recopilación Tutoriales y Manuales Hacking, Seguridad, Privacidad, Hardware, etc


+  Foro de elhacker.net
|-+  Programación
| |-+  Desarrollo Web (Moderador: #!drvy)
| | |-+  ejercico asp con conexión a sql
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: ejercico asp con conexión a sql  (Leído 2,299 veces)
_-Javier-_

Desconectado Desconectado

Mensajes: 18


凸 ◣_◢ ( Javier )◣_◢ 凸


Ver Perfil WWW
ejercico asp con conexión a sql
« en: 18 Marzo 2011, 16:44 pm »

_Este ejercicio consiste en registrar buscar y modificar datos que se encuentran en una base de datos

_utilizaremos Visual studio 2010
_Sql server 2008
_crearemos un nuevo sitio web
aki la interfaz

http://img13.imageshack.us/i/webfya.jpg/

aki el codigo de sql
Código
  1.  
  2. go
  3. use master
  4. go
  5. if(DB_ID('parcialvidarteDelgad')is not null)
  6. drop database parcialvidarteDelgad
  7. go
  8. create database parcialvidarteDelgad
  9. go
  10. use parcialvidarteDelgad
  11. go
  12. create table tipopelicula(
  13. tipo varchar(14)primary key
  14. )
  15. go
  16. insert tipopelicula values ('DRAMA')
  17. insert tipopelicula values ('Suspenso')
  18. insert tipopelicula values ('Terror')
  19. insert tipopelicula values ('CienciaFiccion')
  20. insert tipopelicula values ('Otros')
  21. go
  22.  
  23. create table pelicula(
  24. id int identity primary key,
  25. nombre varchar(30)unique,
  26. tipo varchar(14)foreign key references tipopelicula,
  27. of date,
  28. stock int
  29. )
  30. go
  31. create proc listartipo(@tipo varchar(30))
  32. as begin
  33. select * from pelicula where tipo =@tipo
  34. end
  35. go
  36. create proc registrar(@n varchar(30),@tipo varchar(14),@añof date,@stock int,@msj varchar(60)output)
  37. as begin
  38. if (exists(select * from pelicula where nombre =@n ))
  39. set @msj ='Ya existe pelicula'
  40. else
  41. begin
  42. insert into pelicula values(@n ,@tipo ,@añof ,@stock )
  43. set @msj ='OK'
  44. end
  45. end
  46. GO
  47. create proc MODIFICAR(@id int,@n varchar(30),@tipo varchar(14),@añof date,@stock int,@msj varchar(60)output)
  48. as begin
  49. if (NOT exists(select * from pelicula where id  =@id  ))
  50. set @msj ='no existe pelicula'
  51. else
  52. begin
  53. update pelicula set nombre =@n ,tipo =@tipo ,añof=@añof ,stock =@stock where id  =@id  
  54. set @msj ='OK'
  55. end
  56. end
  57. go
  58. create proc buscar(@id int)
  59. as begin
  60. select * from pelicula where id =@id
  61. end
  62. go
  63. insert into pelicula values('Odisea','DRAMA','10/10/2010',18)
  64. go
  65.  
  66.  

Ahora el codigo en Vb
nota: activamos en la venta de propiedad ispostback=true en la barra de propiedades del combobox o cbxtipo


Código
  1.  
  2. Imports System.Data
  3. Imports System.Data.SqlClient
  4. Partial Class pagina1
  5.    Inherits System.Web.UI.Page
  6.    Private con As New SqlConnection("Server=.;DataBase=parcialvidarteDelgad;Integrated Security=true")
  7.    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click
  8.        Dim cmd As New SqlCommand
  9.        Dim msj As String = ""
  10.        Try
  11.            abrir()
  12.            cmd = New SqlCommand("registrar", con)
  13.            cmd.CommandType = 4
  14.            With cmd.Parameters
  15.                .AddWithValue("@n", txtn.Text)
  16.                .AddWithValue("@tipo", cbxtipo.SelectedValue)
  17.                .AddWithValue("@añof", CDate(txtaño.Text))
  18.                .AddWithValue("@stock", CInt(txtstock.Text))
  19.                .Add("@msj", SqlDbType.VarChar, 60).Direction = 2
  20.            End With
  21.            cmd.ExecuteNonQuery()
  22.            msj = cmd.Parameters("@msj").Value
  23.            MsgBox(msj)
  24.        Catch ex As Exception
  25.            MsgBox(ex.Message)
  26.        End Try
  27.        cerrar()
  28.        limpiar()
  29.    End Sub
  30.    Sub limpiar()
  31.        txtaño.Text = ""
  32.        txtid.Text = ""
  33.        txtn.Text = ""
  34.        txtstock.Text = ""
  35.    End Sub
  36.    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  37.        If Not IsPostBack Then
  38.            listartipos()
  39.        End If
  40.    End Sub
  41.    Sub listartipos()
  42.        Dim dt As New DataTable
  43.        Dim da As SqlDataAdapter
  44.        Try
  45.            abrir()
  46.            da = New SqlDataAdapter("select * from tipopelicula", con)
  47.            da.Fill(dt)
  48.            cbxtipo.DataValueField = "tipo"
  49.            cbxtipo.DataTextField = "tipo"
  50.            cbxtipo.DataSource = dt
  51.            cbxtipo.DataBind()
  52.        Catch ex As Exception
  53.            MsgBox(ex.Message)
  54.        End Try
  55.        cerrar()
  56.    End Sub
  57.    Sub abrir()
  58.        If con.State = 0 Then con.Open()
  59.    End Sub
  60.    Sub cerrar()
  61.        If con.State = 1 Then con.Close()
  62.    End Sub
  63.  
  64.  
  65.  
  66.  
  67.    Protected Sub cbxtipo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxtipo.SelectedIndexChanged
  68.        Dim dt As New DataTable
  69.        Dim da As SqlDataAdapter
  70.        Try
  71.            abrir()
  72.            da = New SqlDataAdapter("listartipo", con)
  73.            da.SelectCommand.CommandType = 4
  74.            da.SelectCommand.Parameters.AddWithValue("@tipo", cbxtipo.SelectedValue)
  75.            da.Fill(dt)
  76.            GridView1.DataSource = dt
  77.            GridView1.DataBind()
  78.        Catch ex As Exception
  79.            MsgBox(ex.Message)
  80.        End Try
  81.        cerrar()
  82.    End Sub
  83.    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
  84.        Dim dt As New DataTable
  85.        Dim da As SqlDataAdapter
  86.        Try
  87.            abrir()
  88.            da = New SqlDataAdapter("buscar", con)
  89.            da.SelectCommand.CommandType = 4
  90.            da.SelectCommand.Parameters.AddWithValue("@id", txtid.Text)
  91.            da.Fill(dt)
  92.            txtid.Text = dt.Rows(0).Item(0).ToString
  93.            txtn.Text = dt.Rows(0).Item(1).ToString
  94.            cbxtipo.Text = dt.Rows(0).Item(2).ToString
  95.            txtaño.Text = dt.Rows(0).Item(3).ToString
  96.            txtstock.Text = dt.Rows(0).Item(4).ToString
  97.        Catch ex As Exception
  98.            MsgBox(ex.Message)
  99.        End Try
  100.        cerrar()
  101.    End Sub
  102.    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  103.        Dim cmd As New SqlCommand
  104.        Dim msj As String = ""
  105.        Try
  106.            abrir()
  107.            cmd = New SqlCommand("MODIFICAR", con)
  108.            cmd.CommandType = 4
  109.            With cmd.Parameters
  110.                .AddWithValue("@id", txtid.Text)
  111.                .AddWithValue("@n", txtn.Text)
  112.                .AddWithValue("@tipo", cbxtipo.SelectedValue)
  113.                .AddWithValue("@añof", CDate(txtaño.Text))
  114.                .AddWithValue("@stock", CInt(txtstock.Text))
  115.                .Add("@msj", SqlDbType.VarChar, 60).Direction = 2
  116.            End With
  117.            cmd.ExecuteNonQuery()
  118.            msj = cmd.Parameters("@msj").Value
  119.            MsgBox(msj)
  120.        Catch ex As Exception
  121.            MsgBox(ex.Message)
  122.        End Try
  123.        cerrar()
  124.        limpiar()
  125.    End Sub
  126.    Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
  127.        limpiar()
  128.    End Sub
  129. End Class
  130.  

eso es todo espero que les haya servido


En línea



Uploaded with ImageShack.us
Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Ejercico 6
Ejercicios
MrYordo 1 4,161 Último mensaje 8 Junio 2005, 20:21 pm
por soplo
Pascal-Ejercico con while do
Ejercicios
Corso 4 20,811 Último mensaje 19 Marzo 2010, 22:29 pm
por steballys
Tnego un ejercico que hize y me presenta 3 errores me pueden ayudar
.NET (C#, VB.NET, ASP)
joalbela 5 3,061 Último mensaje 31 Mayo 2009, 05:29 am
por joalbela
Ejercico en php
PHP
Urk83 6 2,633 Último mensaje 28 Septiembre 2011, 16:51 pm
por Urk83
ejercico matriz
Programación C/C++
eiler 0 1,578 Último mensaje 10 Noviembre 2017, 03:58 am
por eiler
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines