1

我需要一个关于将重复键插入数据库的 sql 帮助。我故意导致异常不将重复记录插入数据库。

在后面的代码中,我有以下代码:

Protected Sub SqlDataSource3_Inserting(sender As Object, e As SqlDataSourceCommandEventArgs)
    Try

    Catch ex As SqlException

    End Try
End Sub

Try...Catch但我不知道该在块中写入什么。我只想显示一个带有文本的 MessageBox你已经是这个组的成员。插入错误页面。

这是我的服务器代码:

<asp:SqlDataSource ID="SqlDataSource3" OnInserted="SqlDataSource3_Inserted" OnInserting="SqlDataSource3_Inserting" runat="server" ConnectionString="<%$ ConnectionStrings:MotWebConnectionString %>" 
    InsertCommand="INSERT INTO [GroupMembers] ([UserName], [GroupId]) VALUES (@UserName, @GroupId)" >

<asp:SqlDataSource ID="SqlDataSource3" OnInserted="SqlDataSource3_Inserted" OnInserting="SqlDataSource3_Inserting" runat="server" ConnectionString="<%$ ConnectionStrings:MotWebConnectionString %>" 
    InsertCommand="INSERT INTO [GroupMembers] ([UserName], [GroupId]) VALUES (@UserName, @GroupId)" >
4

1 回答 1

0

事件处理程序中不会引发异常;相反,您应该执行以下操作:

Protected Sub SqlDataSource3_Inserted(sender As Object, e As SqlDataSourceStatusEventArgs)
   If e.Exception IsNot Nothing Then
      ' Handle exception here
      ' Prevent exception from being re-thrown
      e.ExceptionHandled = True
   End If
End Sub
于 2013-02-11T12:28:48.163 回答