我有一个用于注册新用户的注册表单。创建用户名(电子邮件)和密码并将其插入 mysql DB 没问题。但是,我想知道如何将插入到文本框中的值与名为 Email 的列的值进行比较。
示例我将电子邮件插入文本框:bee@gmail.com 并点击连接到 mysql DB 的下一步按钮。我想比较值,如果数据库表中存在电子邮件:bee@gmail.com,请让用户知道!这可能吗 ?
谢谢
第一步:
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!");
我找到了答案:
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