使用ASP VB检查记录是否存在于DB中

时间:2014-07-13 17:44:38

标签: asp.net sql vb.net

我正在尝试检查我的sql数据库中是否存在来自后面的asp代码的电子邮件

基本上用户会填写表格并提交,我需要在插入之前检查该电子邮件是否存在

 Protected Sub btnSignup_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSignup.Click
    Response.Cookies("survey")("fullname") = TextBoxFullName.Text
    Response.Cookies("survey")("surname") = TextBoxSurname.Text
    Response.Cookies("survey")("lastVisit") = DateTime.Now.ToString()
    Response.Cookies("survey")("contactnumber") = TextBoxPhone.Text
    Response.Cookies("survey")("email") = TextBoxEmail.Text
    Response.Cookies("survey").Expires = DateTime.Now.AddDays(365)


    'InsertCommand="INSERT INTO [Comp_20140409_Broadband] ([SignupName], [SignupGender], [SignupIDNo], [SignupEmailAddress], [CurrentProvider], [CurrentSpeed], [CurrentUsage]) VALUES (@SignupName, @SignupGender, @SignupIDNo, @SignupEmailAddress, @CurrentProvider, @CurrentSpeed, @CurrentUsage)" 
    If Not Page.IsValid Then Exit Sub


    Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
    Dim updateSql3 As String = "Select [PersonId] FROM  [Users] WHERE [Email] = @Email"
    Dim PersonId As Integer
    Using myConnection As New SqlConnection(connectionString)
        myConnection.Open()
        Dim myCommand As New SqlCommand(updateSql3, myConnection)
        myCommand.Parameters.AddWithValue("@Email", TextBoxEmail.Text)
        PersonId = myCommand.ExecuteScalar()



        myConnection.Close()
        End Using



        Dim updateSql2 As String = " INSERT INTO [Survey_Legal] ([LegalInsurance],[ThirdParty], [LegalIssues], [RequestLegal], [PersonId], [Category_Type]) VALUES (@LegalInsurance, @ThirdParty, @LegalIssues, @RequestLegal, @PersonId, @Type )"

        Using myConnection2 As New SqlConnection(connectionString)
        myConnection2.Open()
        Dim myCommand2 As New SqlCommand(updateSql2, myConnection2)
        myCommand2.Parameters.AddWithValue("@LegalInsurance", DDLLegal1.SelectedValue)
        myCommand2.Parameters.AddWithValue("@ThirdParty", DDLLegal2.SelectedValue)
        myCommand2.Parameters.AddWithValue("@LegalIssues", DDLLegal3.SelectedValue)
        myCommand2.Parameters.AddWithValue("@RequestLegal", DDLLegal4.SelectedValue)
        myCommand2.Parameters.AddWithValue("@PersonId", PersonId)
        myCommand2.Parameters.AddWithValue("@Type", "Legal-Insurance")




        myCommand2.ExecuteNonQuery()
        myConnection2.Close()
    End Using

1 个答案:

答案 0 :(得分:0)

这就是我这样做的方式。我在存储过程中使用输出参数检查重复的电子邮件地址。

CREATE Procedure sp_AddSubscriber 
    @Name as nvarchar(50), 
    @Email as nvarchar(50), 
    @AddSubscriber bit OUTPUT

AS

IF (SELECT COUNT(Email) 
    FROM TSubscribers 
    WHERE Email = @Email) = 0

    BEGIN 
        INSERT TSubscribers (Name, Email)  
        VALUES (@Name, @Email)
        SET @AddSubscriber = False                          
    END
ELSE
   SET @AddSubscriber = True

GO