简体   繁体   中英

vb.net mysql compare value of textbox to single value in table's column

I have a registration form that I am using to register new user. No problem creating the username (email) and password and inserting it mysql DB. I am however wondering how would I compare the value inserted into the textbox with value of a column called Email.

Example I insert the email into textbox: bee@gmail.com and hit Next button that connects to mysql DB. I want to compare values and if Email: bee@gmail.com exist in DB table, let user know! Is this possible ?

Thanks

First Step:

Create Procedure FindString(
@MyString nvarchar(50))
As
Begin
Select * From MyTable
Where Value = @MyString
End

Make a class:

public class ReadData
{
    public bool FindString(string myString)
    {
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = "Server=..."; //Your connection string
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandType = CommandType.StoredProcedure;
        command.CommandText = "FindString";
        command.Parameters.AddWithValue("@MyString", myString);
        try
        {
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                return true;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            if (connection.State == ConnectionState.Open)
                connection.Close();
        }                
        return false;
    }
}

Use the class. for example :

ReadData r = new ReadData();

if (r.FindString("Shahingg"))
    MessageBox.Show("I Found it!");
else
    MessageBox.Show("I can't Find it!");

I found the answer:

 Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button7.Click

    con = New MySqlConnection("Database=;" & _
                            "Data Source=;" & _
                            "User Id=;Password=;")


    con.Open()
    Try

        Query = "SELECT Email FROM users WHERE Email='bee@gmail.com'"

        cmd = New MySqlCommand(Query, con)

        reader = cmd.ExecuteReader()

        If reader.HasRows Then
            MessageBox.Show("Email taken")
            '  While reader.Read
            'MysqlData.Text = MysqlData.Text & reader.Item("Email")
            ' End While
        Else
            MessageBox.Show("Email does not exist")
        End If
    Catch ex As Exception


    End Try

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM