【问题标题】:How to stop inserting same record in table using vb.net如何停止使用 vb.net 在表中插入相同的记录
【发布时间】:2009-09-07 08:41:26
【问题描述】:

我在按钮点击事件上使用 VB.NET 和下面的代码。

Protected Sub ibtnSendInvites_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ibtnSendInvites.Click
        Try
            Dim emailList As New List(Of String)
            Dim conString As String = WebConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString
            Dim con As New SqlConnection(conString)
            con.Open()
            Dim cmd As SqlCommand
            For Each curRow As GridViewRow In GridView1.Rows
                Dim chkSelect As CheckBox = CType(curRow.Cells(1).FindControl("chkSelect"), CheckBox)
                Dim emailLabel As Label = CType(curRow.Cells(1).FindControl("lblEmailAddress"), Label)
                If chkSelect.Checked Then
                    emailList.Add(emailLabel.Text)
                    cmd = New SqlCommand("uspInsertDelegate", con)
                    cmd.CommandType = CommandType.StoredProcedure
                    cmd.Parameters.Add("@CourseID", SqlDbType.Int).Value = Session("CourseID")
                    cmd.Parameters.Add("@CPUserID", SqlDbType.Int).Value = CType(curRow.Cells(1).FindControl("lblCPUserID"), Label).Text
                    cmd.Parameters.Add("@StatusID", SqlDbType.Int).Value = 25
                    cmd.Parameters.Add("@CreateUser", SqlDbType.VarChar).Value = Session("LoggedInUser")
                    cmd.ExecuteNonQuery()
                End If
            Next
            For Each email As String In emailList
                Dim message As String = "Please confirm your booking "
                Dim subject As String = "UserPoint Course Booking Invitation Email"
                Dim from As String = "admin@userpoint.com"
                SendEmail.SendMessage(subject, message, from, email, "")
            Next
        Catch ex As Exception

        End Try
    End Sub

如果用户尝试插入具有相同 CourseIDCPUserID 的相同记录,我想抛出异常。

【问题讨论】:

标签: vb.net


【解决方案1】:

Put a unique index 在 DATABASE 表上 在这两列上。如果您尝试插入重复项,则会返回异常。

CREATE UNIQUE INDEX IDX_Course_UserId_Delegate
     ON Delegate (CourseID,CPUserID) 

或者先改变Sp来检查

或者在会引发异常的数据库上添加一个插入触发器。

好啦

 Throw New Exception("CANNOT INSERT DUPLICATE TROW", ex)

【讨论】:

  • 亲爱的,但是我可以有很多课程 ID 不同的 cuserid,反之亦然
  • 没关系。以上只是意味着只允许具有两个值的唯一行,允许任何其他排列。
  • 我在我的 sql server proc 中控制它,如果用户尝试插入相同的记录,我想抛出异常消息
【解决方案2】:

您可以更改存储过程“uspInsertDelegate”,而不是让它引发异常,以在尝试插入之前检查该行是否已存在,并报告它是否已完成。

正如 Preet 所说,我还会在数据库中创建唯一索引,只是为了安全起见。

【讨论】:

  • 我已经在这两个列上创建了唯一索引,并在插入之前使用 IF NOT EXISTS (SELECT * FROM tblDelegate WHERE CourseID = @CourseID and CPUserID = @CPUserID) 在我的 proc 中进行了检查,现在我应该如何向用户显示无法插入记录的消息,因为它已经存在于表中
【解决方案3】:

实际上有两个选项 - 首先查询表以查看该行是否存在并引发自定义异常(将整个事物包装在事务中),或者,在数据库中使用唯一约束/索引强制执行此操作(您应该这样做反正)。然后,当您尝试它时,数据库将返回一个错误...

【讨论】:

    猜你喜欢
    • 2010-11-26
    • 2014-10-11
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2014-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多