จะบันทึกการเปลี่ยนแปลงจาก DataGridView ไปยังฐานข้อมูลได้อย่างไร

ฉันต้องการบันทึกการเปลี่ยนแปลงที่ทำกับ DataGridView ลงในฐานข้อมูล MS SQL CE แต่ฉันทำไม่ได้ การเปลี่ยนแปลงจะไม่ถูกบันทึกลงในฐานข้อมูล....

นี่คือรหัส (VB.net):

    Private Sub Form1_Load(ByVal sender As System.Object, 
        ByVal e As System.EventArgs) handles MyBase.Load

        Dim con As SqlCeConnection = New SqlCeConnection(@"Data  Source=C:\Users\utente\Documents\test.sdf")

        Dim cmd As SqlCeCommand = New SqlCeCommand("SELECT * FROM mytable", con)

        con.Open()
        myDA = New SqlCeDataAdapter(cmd)
        Dim builder As SqlCeCommandBuilder = New SqlCeCommandBuilder(myDA)
        myDataSet = New DataSet()
        myDA.Fill(myDataSet, "MyTable")
        DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
        con.Close()
        con = Nothing

    End Sub



    Private Sub edit_rec()

        Dim txt1, txt2 As String
        Dim indice As Integer = DataGridView1.CurrentRow.Index

        txt1 = DataGridView1(0, indice).Value.ToString '(0 is the first column of datagridview)
        txt2 = DataGridView1(1, indice).Value.ToString '(1 is the second)        MsgBox(txt1 + "  " + txt2)
        '
        DataGridView1(0, indice).Value = "Pippo"
        DataGridView1(1, indice).Value = "Pluto"
        '
        Me.Validate()
        Me.myDA.Update(Me.myDataSet.Tables("MyTable"))
        Me.myDataSet.AcceptChanges()
        '
    End Sub

ขอบคุณสำหรับข้อเสนอแนะใด ๆ


person Pierre Minesso    schedule 23.08.2013    source แหล่งที่มา


คำตอบ (3)


คุณต้องใช้ myDA.Update ซึ่งจะยอมรับการเปลี่ยนแปลง เนื่องจากการเปลี่ยนแปลงใดๆ ที่คุณทำเกิดขึ้นในอินสแตนซ์ภายในเครื่องของชุดข้อมูลนั้น จึงกำจัดทิ้งไปเหมือนกับตัวแปรอื่นๆ

... ฉันเห็นสิ่งนั้นในไฟล์ย่อย edit_rec ของคุณ แต่สิ่งที่เรียกว่า - ไม่มีอะไรในโค้ดที่คุณโพสต์

person guyh92    schedule 23.08.2013

อาจจะช้าไปบ้าง

ในเหตุการณ์การโหลดแบบฟอร์ม คุณเปิดและปิดการเชื่อมต่อ และยิ่งไปกว่านั้น ทั้งหมดเป็นตัวแปรท้องถิ่นที่ทำให้ค่าหรือข้อมูลใด ๆ ลดลงเมื่อออกจากย่อย

Public Class Form1



        Dim con As SqlCeConnection

        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

            con = New SqlCeConnection(@"Data  Source=C:\Users\utente\Documents\test.sdf")

            Dim cmd As SqlCeCommand = New SqlCeCommand("SELECT * FROM mytable", con)

            con.Open()
            myDA = New SqlCeDataAdapter(cmd)
            Dim builder As SqlCeCommandBuilder = New SqlCeCommandBuilder(myDA)
            myDataSet = New DataSet()
            myDA.Fill(myDataSet, "MyTable")
            DataGridView1.DataSource = myDataSet.Tables("MyTable").DefaultView
            cmd.Dispose()

        End Sub

***Put the closing of connection here instead or somewhere else suitable when you don't use it anymore***

        Private Sub Form1_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

            con.Close()
            con = Nothing

        End Sub

        Private Sub edit_rec()

            Dim txt1, txt2 As String
            Dim indice As Integer = DataGridView1.CurrentRow.Index

            txt1 = DataGridView1(0, indice).Value.ToString '(0 is the first column of datagridview)
            txt2 = DataGridView1(1, indice).Value.ToString '(1 is the second)        MsgBox(txt1 + "  " + txt2)
            '
            DataGridView1(0, indice).Value = "Pippo"
            DataGridView1(1, indice).Value = "Pluto"
            'You code to update goes here and not in my scope of answer

       End Sub

    End Class
person user3571272    schedule 25.04.2014

ฉันคิดว่าคุณต้องการเพิ่ม @ ด้วยสตริงการเชื่อมต่อของคุณ

SqlConnection con;

      con = New SqlConnection(@"Data  Source=C:\Users\utente\Documents\test.sdf");
person Kishan    schedule 11.09.2014