en mi caso, dejare el nombre por defecto de la clase, tal como aparece en la figura:
Class Kode
y en la clase copiamos el código siguiente
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Public Class Class1
Dim sql As SqlConnection
Dim leer As SqlDataReader
Dim cmd As SqlCommand
Dim form2 As New Entrada
Dim da As SqlDataAdapter
Dim dt As DataTable
Dim cnn As String = ConfigurationManager.ConnectionStrings("con").ConnectionString
Public Sub VerificarUsuario(ByVal username, ByVal password)
Using sql = New SqlConnection(cnn)
sql.Open()
cmd = New SqlCommand
cmd.CommandText = "select * from registro where username=@user and password=@password"
cmd.Connection = sql
cmd.Parameters.AddWithValue("@user", username)
cmd.Parameters.AddWithValue("@password", password)
leer = cmd.ExecuteReader
If leer.Read Then
leer.Close()
cmd.Dispose()
form2.Show()
Form1.Close()
Form1.Dispose()
Else
MessageBox.Show("Nombre de usuario o contraseña incorrectas", "Error")
leer.Close()
cmd.Dispose()
End If
End Using
End Sub
Public Function Datos() As DataTable
Dim cadena As String = "select * from registro"
dt = New DataTable
Using sql = New SqlConnection(My.Settings.con)
Using da = New SqlDataAdapter(cadena, sql)
da.Fill(dt)
Return dt
End Using
End Using
End Function
Public Sub InsertarRegistro(ByVal val1, ByVal val2)
Using sql = New SqlConnection(cnn)
Using cmd = New SqlCommand
sql.Open()
cmd.Connection = sql
cmd.CommandText = "insert into registro values(@user,@pass)"
cmd.Parameters.AddWithValue("@user", val1)
cmd.Parameters.AddWithValue("@pass", val2)
cmd.ExecuteNonQuery()
MessageBox.Show("se han ingresado los datos correctamente")
End Using
End Using
End Sub
Public Sub ModificarDatos(ByVal val1, ByVal val2, ByVal val3)
Using sql = New SqlConnection(cnn)
Using cmd = New SqlCommand
sql.Open()
cmd.Connection = sql
cmd.CommandText = "update registro set username=@user, password=@pass where id=@id"
cmd.Parameters.AddWithValue("@user", val1)
cmd.Parameters.AddWithValue("@pass", val2)
cmd.Parameters.AddWithValue("@id", val3)
cmd.ExecuteNonQuery()
MessageBox.Show("Se han modificado los datos correctamente")
End Using
End Using
End Sub
Public Sub EliminarDatos(ByVal val1)
Using sql = New SqlConnection(cnn)
Using cmd = New SqlCommand
sql.Open()
cmd.Connection = sql
cmd.CommandText = "delete from registro where id=@id"
cmd.Parameters.AddWithValue("@id", val1)
cmd.ExecuteNonQuery()
MessageBox.Show("datos eliminados correctamente")
End Using
End Using
End Sub
End Class
Login Kode
El código de login queda de la siguiente forma:
Public Class Form1
Dim k As New Class1
Private Sub Ingresar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ingresar.Click
k.VerificarUsuario(TextBox1.Text, TextBox2.Text)
End Sub
End Class
Public Class Entrada
Dim u As New Class1
Dim bs As BindingSource
Public Sub CargarDatos()
bs = New BindingSource
bs.DataSource = u.Datos()
BindingNavigator1.BindingSource = bs
DataGridView1.DataSource = bs
TextBox1.DataBindings.Clear()
TextBox2.DataBindings.Clear()
TextBox3.DataBindings.Clear()
TextBox1.DataBindings.Add("text", bs, "username")
TextBox2.DataBindings.Add("text", bs, "password")
TextBox3.DataBindings.Add("text", bs, "id")
End Sub
Private Sub Consultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Consultar.Click
CargarDatos()
End Sub
Private Sub BIngresar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BIngresar.Click
u.InsertarRegistro(TextBox1.Text, TextBox2.Text)
limpiar()
End Sub
Private Sub BModificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BModificar.Click
u.ModificarDatos(TextBox1.Text, TextBox2.Text, TextBox3.Text)
limpiar()
End Sub
Private Sub limpiar()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
DataGridView1.DataSource = DBNull.Value
End Sub
Private Sub BEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BEliminar.Click
u.EliminarDatos(TextBox3.Text)
limpiar()
End Sub
End Class