Login Visual Basic with database


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()


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

        sqlcmd.CommandText = sqltext
        sqlcmd.Connection = sqlcon

    Catch ex As Exception
    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

1 answer


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;
      return true;

I recommend you encrypt your password.

answered by 08.05.2017 / 17:54