Para los q empiezan y quieren practicar

Iniciado por _-Javier-_, 18 Marzo 2011, 16:06 PM

0 Miembros y 1 Visitante están viendo este tema.

_-Javier-_

Realizaremos un ejercio simple de conexión VB~SQL
el cual consistira en registrar,modificar y eliminar docentes desde vb
_utilizare visual studio 2010 y sql server 2008
_crearemos un nuevo proyecto de tipo formulario windows
aki la interfaz


http://img215.imageshack.us/i/interfazk.jpg/


_Luego mostraremos el codigo en SQL
 
Código (vbnet) [Seleccionar]


go
use master
go
if(db_id('practicando')is not null)
drop database practicando
go
create database practicando
go
use practicando
go
create table docente(
dni char(8)primary key,
n varchar(30)not null,
ape varchar(30)not null,
sexo char(1)not null check(sexo='M'or sexo='F'),
edad tinyint)
go
create proc registrar(@dni char(8),@n varchar(30),@ape varchar(30),
@sexo char(1),@edad tinyint,@msj varchar(60)output)
as begin
if(exists(select * from docente where dni=dni ))
set @msj ='dni ya existe'
ELSE
BEGIN
insert into docente values(@dni ,@n ,@ape ,@sexo ,@edad )
set @msj ='REGISTRADO OK'
END
end



_ahora el codigo en Vb

Código (vbnet) [Seleccionar]


Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
   Private con As New SqlConnection("Server=.;DataBase=practicando;Integrated Security=true")
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       rbtm.Checked = True
       listar()
       txtregistros.Text = DataGridView1.Rows.Count - 1
       lblhora.Text = TimeOfDay
   End Sub
   Sub listar()
       Dim dt As New DataTable
       Dim da As SqlDataAdapter
       Try
           abrir()
           da = New SqlDataAdapter("select * from docente", con)
           da.Fill(dt)
           DataGridView1.DataSource = dt
       Catch ex As Exception : MsgBox(ex.Message)
       End Try
       cerrar()
       txtregistros.Text = DataGridView1.Rows.Count - 1
   End Sub
   Sub abrir()
       If con.State = 0 Then con.Open()
   End Sub
   Sub cerrar()
       If con.State = 1 Then con.Close()
   End Sub
   Sub limpiar()
       txtape.Clear()
       TXTDNI.Clear()
       txtedad.Clear()
       txtnom.Clear()
       rbtm.Checked = True
   End Sub

   Private Sub btnregistrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnregistrar.Click
       Dim cmd As SqlCommand
       Dim msj As String = ""
       Try
           abrir()
           cmd = New SqlCommand("registrar", con)
           cmd.CommandType = 4
           With cmd.Parameters
               .AddWithValue("@dni", TXTDNI.Text)
               .AddWithValue("@n", txtnom.Text)
               .AddWithValue("@ape", txtape.Text)
               If rbtm.Checked = True Then
                   .AddWithValue("@sexo", "M")
               Else
                   .AddWithValue("@sexo", "F")
               End If
               .AddWithValue("@edad", txtedad.Text)
               .Add("@msj", SqlDbType.VarChar, 60).Direction = 2
           End With
           cmd.ExecuteNonQuery()
           msj = cmd.Parameters("@msj").Value
           MessageBox.Show(msj)
       Catch ex As Exception : MessageBox.Show(ex.Message)

       End Try
       cerrar()
       limpiar()
       listar()
   End Sub
End Class


Ahora los q quieran hagan los 2 botones mas
Tomar en cuenta que los proc de eliminar y modificar son similares al registrar


Uploaded with ImageShack.us