SQL适配器。如何更新没有主键的DataTable

时间:2013-06-25 00:47:34

标签: vb.net winforms datagridview sql-update sqldataadapter

我创建的这个表没有主键。有一个原因是它没有按键创建。它类似于产品和客户关系表。因此,在使用SqlDataAdapterDataSet以及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 the DataSet`以及适用于其他表单的更新代码处理多个表单。

更新代码:

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

1 个答案:

答案 0 :(得分:0)

所以我决定继续回答我的问题。您无法使用上面的代码,但您仍然可以插入新行。由于数据集是一个内存,如果整个数据库被删除它将不起作用。因此,如何更新没有主键或复合键的表来回转它,然后将数据集表中的所有行插入其中。这是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()