【问题标题】:how to clear previous session value to avoid duplicate records insert to mysql如何清除以前的会话值以避免重复记录插入到 mysql
【发布时间】:2015-04-19 10:59:54
【问题描述】:

我正在使用数据表将新行插入到 gridview 中。

我正在使用 session 来存储以前的值,因为不想用 gridview 中的当前新行覆盖上一行。

当我单击按钮单击以插入另一新行时,gridview 中的结果显示能够逐行显示,但在 mysql 数据库中插入现有行和前一行数据导致重复行。

当我插入另一个新行时,如何使用会话清除之前的行值?

下面是我的代码,谁能指导我哪里出了问题?

     Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click
    lblVisualWarningCode.Visible = False


    Dim conString As String = ConfigurationManager.ConnectionStrings("prcConnectionString").ConnectionString
    Dim MySql As New MySqlConnection(conString)
    Dim cmd As New MySqlCommand("", MySql)
    MySql.Open()
    cmd.CommandText = "SELECT RepairID FROM tbVisual ORDER BY CAST(RepairID AS UNSIGNED) DESC"

    Dim a As String = cmd.ExecuteScalar
    Dim str As Integer = a
    Dim dt As New DataTable("Visual")

    Try
           If Session("Visual") Is Nothing Then
                dt.Columns.Add("RepairID", GetType(String))
                dt.Columns.Add("VisualInspectionErrorCode", GetType(String))
                dt.Columns.Add("VisualInspectionActionTaken", GetType(String))
                dt.Columns.Add("VisualInspectionComponentLocation", GetType(String))
            Else
                dt = DirectCast(Session("Visual"), DataTable)
            End If

            Session("Visual").Clear()'i am not sure where to put this
             'lbVisual is listbox
            For i As Integer = 0 To lbVisual.Items.Count - 1
                str = str + 1
                Dim dr As DataRow = dt.NewRow()
                dr("RepairID") = str.ToString
                dr("VisualInspectionErrorCode") = ddlVisualInspectionErrorCode.SelectedValue
                dr("VisualInspectionActionTaken") = tbVisualInspectionActionTaken.Text
                dr("VisualInspectionComponentLocation") = lbVisual.Items(i).ToString
                dt.Rows.Add(dr)

            Next


        Catch ex As Exception
            Response.Write(ex.Message)
        End Try


        For Each row As DataRow In dt.Rows
            Dim query1 As String = "INSERT INTO tbvisual (RepairID,VisualInspectionErrorCode,VisualInspectionActionTaken,VisualInspectionComponentLocation) " &
                "VALUES ('" + row("RepairID") + "','" + row("VisualInspectionErrorCode") + "','" + row("VisualInspectionActionTaken") + "','" + row("VisualInspectionComponentLocation") + "')"
            cmd = New MySqlCommand(query1, MySql)
            cmd.ExecuteNonQuery()
        Next



    Session("Visual") = dt

    MySql.Close()
    gvVisual.DataSource = dt
    gvVisual.DataBind()

End Sub

【问题讨论】:

  • 您每次都在插入所有行,这很正常,您的数据库中有重复的行。首先,您应该向表中添加一个主键,以便每次尝试插入现有行时都会出现异常。然后你必须注意只在数据库中插入新行。只要您一次添加一行,您就可以简单地插入最后一行。
  • 实际上你只需要在每次插入后从 dt 中删除该行,然后将其存储在会话中

标签: mysql asp.net gridview


【解决方案1】:

您可以在您的 MYSQL 表上创建唯一索引以防止插入重复的行并在插入数据时编写 "INSERT IGNORE" mysql 查询,如果已经存在,它将忽略行。

点击以下链接了解如何在 MYSQL 中创建唯一索引- http://www.w3schools.com/sql/sql_unique.asp

这里是了解“INSERT IGNORE”MYSQL 查询的链接:- http://www.tutorialspoint.com/mysql/mysql-handling-duplicates.htm

【讨论】:

    【解决方案2】:

    而不是Session("Visual").Clear() 使用dt.Clear(),这是因为一旦您的会话被创建,它就包含有行和列的数据表。 现在,在每次迭代中,您只希望在每次插入后保留列并删除行。

    dt.Clear() 将删除所有行,因此您将只有每个插入的新行


    更新:代码

    If Session("Visual") Is Nothing Then
                    dt.Columns.Add("RepairID", GetType(String))
                    dt.Columns.Add("VisualInspectionErrorCode", GetType(String))
                    dt.Columns.Add("VisualInspectionActionTaken", GetType(String))
                    dt.Columns.Add("VisualInspectionComponentLocation", GetType(String))
                Else
                    dt = DirectCast(Session("Visual"), DataTable)
                End If
    
                dt.Clear()
                 'lbVisual is listbox
                For i As Integer = 0 To lbVisual.Items.Count - 1
                    str = str + 1
                    Dim dr As DataRow = dt.NewRow()
                    dr("RepairID") = str.ToString
                    dr("VisualInspectionErrorCode") = ddlVisualInspectionErrorCode.SelectedValue
                    dr("VisualInspectionActionTaken") = tbVisualInspectionActionTaken.Text
                    dr("VisualInspectionComponentLocation") = lbVisual.Items(i).ToString
                    dt.Rows.Add(dr)
    
                Next
    

    【讨论】:

    • tushar 感谢您的回复,但是在哪里可以找到 dt.clear()?你能给我看一些编码吗?
    • 只需将其替换为您编写的 `Session("Visual").Clear()' 会及时更新答案
    • tushar,我添加了,但它又回到了我之前的问题,即最新的当前行覆盖了上一行..无法逐行显示 1 行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多