Login Visual Basic with database

2

Good, you see I am creating a login in visual studio using SQL Server Compact but I do not know how to obtain the data resulting from the sentence and compare them, I have this:

 Dim sqlcon As New SqlCeConnection("Data Source=C:\Usuarios.sdf; Persist Security Info=False;")
 Dim sqlcmd As New SqlCeCommand()

    Try

        Dim sqltext As String
        sqltext = "Select * from Usuarios where Nombre = '" & TextBox1.Text & "' and Contra = '" + TextBox2.Text + "'"

        sqlcmd.CommandText = sqltext
        sqlcmd.Connection = sqlcon
        sqlcon.Open()
        sqlcmd.ExecuteNonQuery()


        sqlcon.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

It's the first time I use it and I'm a little green, I hope you can help me. Basically what I want to do is if a record has been found, that is, True, access another Form. Greetings.

    
asked by som1995 08.05.2017 в 17:47
source

1 answer

1

To start you have to make a count in your query so that it returns you in value 1 if it is correct or 0 if it is erroneous, for example:

public bool ValidacionUsuario(string varnombre, string varpassword)
{
     sqlcmd.CommandText = "Select Count(*) from Usuarios where Nombre = '" & varnombre & "' and Contra = '" + varpassword + "'";
     sqlcmd.CommandType = CommandType.Text;
     sqlcmd.Connection = sqlcon;
     int count = Convert.ToInt32(sqlcmd.ExecuteScalar());
     if(count == 0)
      {
     return false;
      }
      else
      {
      return true;
      }
}

I recommend you encrypt your password.

    
answered by 08.05.2017 / 17:54
source