查看是否存在电子邮件

时间:2017-05-15 08:04:13

标签: vb.net

我可以创建注册,但我不确定如何检查数据库中是否已存在电子邮件。我是否必须首先搜索数据库,或者我可以使用一行代码?

MyConn = New OleDbConnection
MyConn.ConnectionString = connString 
MyConn.Open() 

Dim cmd1 As OleDbCommand = New OleDbCommand(str1, MyConn)
str1 = "insert into UserData ([First Name], [Last Name], [Email], [Address], [Phone Number], [Username], [Password]) values (?, ?, ?, ?, ?, ?, ?)"

cmd1.Parameters.Add(New OleDbParameter("First Name", CType(TxtFirstNameReg.Text, String))) 
cmd1.Parameters.Add(New OleDbParameter("Last Name", CType(TxtLastNameReg.Text, String))) 
cmd1.Parameters.Add(New OleDbParameter("Email", CType(TxtEmailReg.Text, String))) 

cmd1.Parameters.Add(New OleDbParameter("Address", CType(TxtAddress.Text, String))) 

cmd1.Parameters.Add(New OleDbParameter("Phone Number", CType(TxtPhoneNumReg.Text, String))) 
cmd1.Parameters.Add(New OleDbParameter("Username", CType(TxtUsernameReg.Text, String))) 

cmd1.Parameters.Add(New OleDbParameter("Password", CType(TxtPasswordReg.Text, String))) 

Try 

    cmd1.ExecuteNonQuery() 
    cmd1.Dispose() 

    MsgBox("Your account has been created") 

    Dim objOutlook As Object 
    Dim objOutlookMsg As Object 
    objOutlook = CreateObject("Outlook.Application") 
    objOutlookMsg = objOutlook.CreateItem(0)
    With objOutlookMsg
        .To = TxtEmailReg.Text
        .Subject = "Registration Confirmation"
        .Body = "Thank you for registering with Hotel Booking System, your login details for this account are as follows" & Environment.NewLine & Environment.NewLine & " Username: " & TxtUsernameReg.Text & Environment.NewLine & " Password: " & TxtPasswordReg.Text 
        .Send() 
    End With
    objOutlookMsg = Nothing 
    objOutlook = Nothing 


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

1 个答案:

答案 0 :(得分:0)

我会首先检查数据库以查看电子邮件地址是否存在:

Using con As New OleDbConnection(connString ),
      cmd As New OleDbCommand("SELECT * FROM [UserData] WHERE [Email] = ?", con)

 cmd.Parameters.Add(New OleDbParameter("@Email", OleDbType.VarChar)).Value = TxtEmailReg.Text

  con.Open()

  If cmd.ExecuteReader().HasRows Then
     'Record exists
  End If

End Using

请注意,我已实施Using

  

有时,您的代码需要非托管资源,例如文件句柄,COM包装器或SQL连接。使用块可确保在代码完成后处理一个或多个此类资源。这使得它们可供其他代码使用。

我还会在传递参数时指定数据类型。在你的情况下,它可能并不重要,但我仍然会考虑使用OleDbParameter Constructor (String, OleDbType)来添加你的参数。在这一点上,值得注意的是,当使用OleDbParameter时,重要的顺序不是名称。您已经拥有了正确的订单,但希望将其指出以供将来参考。

您也不需要像现在这样使用CType.Text属性已经是String类型,因此在这种情况下使用CType实际上并没有做任何事情。

请注意,ExecuteNonQuery会告诉您受影响的行数。值得检查一下。

在处理Outlook时,请考虑使用Microsoft.Office.Interop.Outlook命名空间直接使用对象。你必须下载这些,但它很简单。只要一个谷歌。我还会看一下实现一些代码来帮助释放这些对象。此code基于Siddharth Rout回答:

Private Sub ReleaseObject(ByVal obj As Object)
    Try
        Dim intRel As Integer = 0
        Do
            intRel = System.Runtime.InteropServices.Marshal.ReleaseComObject(obj)
        Loop While intRel > 0
        obj = Nothing
    Catch ex As Exception
        obj = Nothing
    Finally
        GC.Collect()
    End Try
End Sub

我已经整理了一些示例代码,希望能给你一个良好的开端:

Dim addressExists As Boolean = False
Dim userAdded As Boolean = False

Using con As New OleDbConnection(connString)
    Using cmd As New OleDbCommand("SELECT * FROM [UserData] WHERE [Email] = ?", con)

        cmd.Parameters.Add(New OleDbParameter("@Email", OleDbType.VarChar)).Value = TxtEmailReg.Text

        con.Open()

        If cmd.ExecuteReader().HasRows Then
            addressExists = True
        End If

    End Using

    If addressExists Then
        MessageBox.Show("Email address already exists.", "Email Exists", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Else
        Using cmd As New OleDbCommand("INSERT INTO [UserData] ([First Name], [Last Name], [Email], [Address], [Phone Number], [Username], [Password]) VALUES(?, ?, ?, ?, ?, ?, ?)", con)

            cmd.Parameters.Add(New OleDbParameter("@FirstName", OleDbType.VarChar)).Value = TxtFirstNameReg.Text
            cmd.Parameters.Add(New OleDbParameter("@LastName", OleDbType.VarChar)).Value = TxtLastNameReg.Text
            cmd.Parameters.Add(New OleDbParameter("@Email", OleDbType.VarChar)).Value = TxtEmailReg.Text
            cmd.Parameters.Add(New OleDbParameter("@Address", OleDbType.VarChar)).Value = TxtAddress.Text
            cmd.Parameters.Add(New OleDbParameter("@PhoneNumber", OleDbType.VarChar)).Value = TxtPhoneNumReg.Text
            cmd.Parameters.Add(New OleDbParameter("@Username", OleDbType.VarChar)).Value = TxtUsernameReg.Text
            cmd.Parameters.Add(New OleDbParameter("@Password", OleDbType.VarChar)).Value = TxtPasswordReg.Text

            If cmd.ExecuteNonQuery() = 1 Then
                userAdded = True
            End If
        End Using
    End If
End Using

If userAdded Then

    MessageBox.Show("Your account has been created", "Account Created", MessageBoxButtons.OK, MessageBoxIcon.Information)

    Dim oApp As New Outlook.Application
    Dim oMessage As Outlook.MailItem = DirectCast(oApp.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem)
    With oMessage
        .To = TxtEmailReg.Text
        .Subject = "Registration Confirmation"
        .Body = .Body = "Thank you for registering with Hotel Booking System, your login details for this account are as follows" & Environment.NewLine & Environment.NewLine & " Username: " & TxtUsernameReg.Text & Environment.NewLine & " Password: " & TxtPasswordReg.Text
        .Send()
    End With

    ReleaseObject(oMessage)
    ReleaseObject(oApp)

End If

这不在本问题的范围之内,但我也会考虑加密密码。将它们存储为纯文本是不好的做法。看看SO问题; Best way to store password in database,可能会就如何做到这一点给你一些想法。

我希望这会让你有所作为。