aver si les interesa este ejercicio de logeo con conexión a sql
abrimos sql server 2008 y creamos una ew query y aplicamos el siguiente :
Código
go use master go if (DB_ID('tienda')is not null) drop database tienda go create database tienda go use tienda go sp_helpdb tienda go create table users (dni char(8)primary key, clave varchar(8)not null, ape varchar(30)not null, nom varchar(30)not null) go --------------------procedimiento loguear create proc loguear(@dni char(8),@clave varchar(8),@datos varchar(62)output) as begin if(exists(select * from users where dni=@dni and clave=@clave )) set @datos =(select ape +','+nom from users where dni=@dni ) else set @datos ='datos incorrectos' end go insert users values('10203040','myPass','Rios flores','Jose'), ('44556677','asd123','Paz gomes','Cinthia') go declare @datos varchar(62) exec loguear '10203040','myPass',@datos output select @datos go declare @datos varchar(62) exec loguear '44556677','asd123',@datos output select @datos go
2_abrimos visual studio 2008 y creamos un new project(formulario windows)
2.1_ le agregamos un modulo con el siguiente:
Código
] Imports System.Data Imports System.Data.SqlClient Module Module1 Public Con As New SqlConnection("Data Source=.;DataBase=tienda;Integrated Security=true") Public Sub abrir() If Con.State = 0 Then Con.Open() End Sub Sub cerrar() If Con.State = 1 Then Con.Close() End Sub Public clave As String End Module
3_ver interfaz del formulario
utilizaremos 2 botones y 2 textbox (txtdni-txtclave) y escribimos el siguiente
Código
] Imports System.Data Imports System.Data.SqlClient Public Class acceso Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ingresar() txtdni.Text = "" txtclave.Text = "" End Sub Sub ingresar() Dim cmd As SqlCommand Dim msj As String Try Call abrir() cmd = New SqlCommand("loguear", Con) cmd.CommandType = CommandType.StoredProcedure With cmd.Parameters .AddWithValue("@dni", txtdni.Text) .AddWithValue("@clave", txtclave.Text) .Add("@datos", SqlDbType.VarChar, 62).Direction = 2 End With cmd.ExecuteNonQuery() msj = cmd.Parameters("@datos").Value MessageBox.Show(msj) clave = cmd.Parameters("@datos").Value Me.Hide() My.Forms.accesoactivo.ShowDialog() Catch ex As Exception MessageBox.Show(ex.Message) End Try Call cerrar() End Sub End Class
4_ahora agregamos un nuevo formulario llamado accesoactivo y aplicamos el siguiente.
pero antes vemos la interfaz:(utilizando un toolstrip1 y le agregamos un toolstriptextbox1 llamado ttxtdatos(propiedad name))
Código
-Listo . hemos terminado de hacer un logeo con BD .
] Imports System.Data Imports System.Data.SqlClient Public Class accesoactivo Private Sub accesoactivo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ttxtdatos.Text = clave End Sub End Class
_Despues de logearnos aparecera el nombre del dueño del dni en el ttxtdatos
_dni y pass estan en la BD (select * from users)
_bueno es todo ojala les sirva