0

我创建的这个表没有主键。没有键创建它是有原因的。它类似于产品和客户关系表。SqlDataAdapter因此,在使用并DataSet填充DataTable的标准程序之后,DataGrid我在更新更改时出错。

我一直在使用DataGrid' but they all work fine due to the fact the table have primary keys. I tried adding a composite key but it didn't work. So below is my code for theDataSet` 和适用于其他表单的更新代码处理几种表单。

更新代码:

cmdbuilder = New SqlCommandBuilder(adapter)

If primaryDS IsNot Nothing Then
    primaryDS.GetChanges()

    'update changes
    adapter.Update(primaryDS)
    MsgBox("Changes Done")

    'refresh the grid
    CMDrefresh()
End If

这是DataTable我尝试添加 5 个复合键的编码。那么你如何更新这个问题呢?

Try
    myconnection = New SqlConnection(strConnection)
    myconnection.Open()

    adapter = New SqlDataAdapter(StrQuery, myconnection)
    adoPrimaryRS = New DataSet

    adapter.Fill(primaryDS)
    Dim mainTable As DataTable = primaryDS.Tables(0)

    DataGrid.AutoGenerateColumns = False
    mainTable.PrimaryKey = New DataColumn() {mainTable.Columns(0), _
                                             mainTable.Columns(1), _
                                             mainTable.Columns(2), _
                                             mainTable.Columns(3), _
                                             mainTable.Columns(4)}

    bndSrc.DataSource = mainTable
    DataGrid.DataSource = bndSrc

    gDB.Connection.Close()

Catch ex As Exception
    MsgBox(ex.Message)
End Try
4

1 回答 1

0

所以我决定继续回答我的问题。您不能使用上面的代码 up 但您仍然可以插入新行。由于数据集是一个内存,如果整个数据库被删除,它就不会生效。因此,如何更新没有主键或复合键的表的答案是为了将其截断,然后将数据集表中的所有行插入其中。这是 Trancute 的代码,下面是插入的代码。有了这些,表格就获得了新的价值。这个对我有用。

    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    con.ConnectionString = strConnection
    Dim strSql As String
    'MsgBox(con.ConnectionString.ToString)
    Try


        con.Open()
        cmd = New SqlCommand
        cmd.Connection = con
        strSql = "TRUNCATE TABLE Table1"
        cmd.CommandText = strSql
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        cmd = Nothing
        con.Close()


    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

所以这里是插入代码。

             Dim con As New SqlConnection
    Dim cmd As New SqlCommand



    Dim strSql As String
    con.ConnectionString = strConnection
    For i As Integer = 0 To grdDataGrid.Rows.Count - 1

        'MsgBox(con.ConnectionString.ToString)
        con.Open()
        cmd = New SqlCommand
        cmd.Connection = con
        Try




            strSql = "INSERT INTO Table1 ( [one],  [two], [three], [four], [five] )" +_ 
            "VALUES (@one, @two, @three ,@four ,@five  )"

            cmd.CommandText = strSql
            cmd.Parameters.AddWithValue("@one", grdDataGrid.Rows(i).Cells(2).Value)
            cmd.Parameters.AddWithValue("@two", grdDataGrid.Rows(i).Cells(0).Value)
            cmd.Parameters.AddWithValue("@three", grdDataGrid.Rows(i).Cells(1).Value)
            cmd.Parameters.AddWithValue("@four", grdDataGrid.Rows(i).Cells(3).Value)
            cmd.Parameters.AddWithValue("@five", grdDataGrid.Rows(i).Cells(4).Value)

            cmd.ExecuteNonQuery()
            cmd.Dispose()
            cmd = Nothing
            con.Close()


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    Next
    CMDrefresh()
于 2013-06-26T02:10:53.717 回答