【问题标题】:How to insert database from datagridview not duplicate value. [VB.NET]如何从 datagridview 插入数据库而不是重复值。 [VB.NET]
【发布时间】:2014-06-26 03:49:05
【问题描述】:

我将数据添加到 datagridview,检查 datagridview 中的重复值并插入数据库。

例如

Datagridview
Code:
130027646001
130027646002
130027646003

After add data to datagridview
Code:
130027646001
130027646002
130027646003
130027646004
130027646005

After to insert to database Result
130027646001
130027646002
130027646003
130027646001
130027646002
130027646003
130027646004
130027646005

But I want Result
130027646001
130027646002
130027646003
130027646004
130027646005

我想插入数据库不要将值仅 130027646004,130027646005 复制到数据库。 我尝试编写代码 2 类型但并非全部工作。

此代码(类型一):

    Dim sqlC As String = ""
    Dim DT As New DataTable
    Dim sendStatus As Integer

    sqlC = "Select c.* "
    sqlC &= "FROM Clother c 
    sqlC &= "WHERE c.No=" & tClother
    DT = FShowData(sqlItem)

    If DT.Rows.Count > 0 Then
        For i As Integer = 0 To DgvItem.Rows.Count - 1
            If DgvItem.Rows(i).Cells(0).Value <> DT.Rows(i).Item("Code") Then
                sendStatus = FuncInsert(CStr(DgvItem.Rows(i).Cells(0).Value))
            End If
        Next
    End If

此代码(类型二): 我编写代码但错误“对于 Int32,值太大或太小。无法在无列中存储 。预期类型为 Int32。”

        Dim sqlC As String = ""
        Dim DT As New DataTable
        Dim sendStatus As Integer

        sqlC = "Select c.* "
        sqlC &= "FROM Clother c 
        sqlC &= "WHERE c.No=" & tClother
        DT = FShowData(sqlItem)

If DgvItem.Rows.Count > 0 Then
            For i As Integer = 0 To DgvItem.Rows.Count - 1
                If (CStr(DgvItem.Rows(i).Cells("Code").Value) <> "") Then
                    Dim dr As DataGridViewRow
                    dr = DgvItem.Rows(i)
                    DT.TableName = "Code"
                    If DT.Rows.Count > 0 Then
                        For j As Integer = 0 To DT.Rows.Count - 1
                            If CStr(DgvItem.Rows(i).Cells("Code").Value) <> CStr(DT.Rows(j).Item("Code")) Then
                                sendStatus = FuncInsert(CStr(DgvItem.Rows(i).Cells(0).Value))
                            End If
                        Next
                    End If

                    DT.Rows.Add(dr.Cells("Code").Value).ToString() >> Error Line: Value was either too large or too small for an Int32.Couldn't store <130027646001> in No Column.  Expected type is Int32.

                End If
            Next
        End If
        DT.Dispose()
        DT = Nothing

桌布

Field Type
Code  nvarchar

【问题讨论】:

  • 我不知道你在问什么。
  • 我同意米奇的观点,这有点难以理解。但是您是否尝试过使用SELECT DISTINCT?你可以阅读更多关于它here

标签: vb.net datagridview datatable duplicates


【解决方案1】:

为什么不在数据库表中使用自动增量和主键?插入重复项似乎您没有每个字段的唯一值。

如果你有这样的表:

第 1 列 | 1 2 3 4

并且您想在使用 NEXTVAL 的当前值之后自动添加 1 个值。我现在可能很明确,但我希望你能明白。

您也可以查看此链接:http://publib.boulder.ibm.com/infocenter/idshelp/v10/index.jsp?topic=/com.ibm.sqls.doc/sqls1016.htm

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 2014-07-09
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多