Вопрос

This table that I have has been created with no primary keys. There is a reason why its been created with no keys. It is something like a product and customer relationship table. So after the standard procedure of using SqlDataAdapter and DataSet along with DataTable to fill the DataGrid I have an error updating the changes.

I have been working on several forms using 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` and the update code which works for other forms.

The update codes:

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

And here is the coding for the DataTable I tried adding 5 composite keys. So how do you update with this problem?

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
Это было полезно?

Решение

So I decided to go on and answer my question. You cant use the code above to up but you can still insert the new rows. Since Dataset is a memory if the whole database was removed it would not be effect. So the answer to how to update a table with no primary key or composite keys it to trancute it then insert all rows from the Dataset Table in to it. Here is the Code for Trancute and The one below is to insert. With these the table gets new values. It works for me.

    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

So here is The Insert code.

             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()
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top