0

我将数据添加到 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,值太大或太小。无法在无列中存储 <130027646001>。预期类型是 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
4

1 回答 1

0

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

如果你有这样的表:

第 1 列 | 1 2 3 4

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

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

于 2013-07-28T19:36:17.650 回答