vb.net mysql将textbox的值与表的列中的单个值进行比较

时间:2013-01-14 19:46:37

标签: c# mysql wpf vb.net email

我有一个注册表单,用于注册新用户。创建用户名(电子邮件)和密码并插入mysql DB没问题。然而,我想知道如何将插入文本框的值与名为Email的列的值进行比较。

示例I将电子邮件插入文本框:bee@gmail.com并点击连接到mysql DB的Next按钮。我想比较值,如果在DB表中存在Email:bee@gmail.com,请让用户知道!这可能吗?

由于

2 个答案:

答案 0 :(得分:1)

第一步:

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

上课:

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;
    }
}

使用课程。例如:

ReadData r = new ReadData();

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

答案 1 :(得分:0)

我找到了答案:

 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