如何将更改从DataGridView保存到数据库? [英] How to save changes from DataGridView to the database?

查看:85
本文介绍了如何将更改从DataGridView保存到数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将对DataGridView所做的更改保存到数据库MS SQL CE中,
,但是我不能,所做的更改未保存到数据库中。...

I would like to save the changes made to a DataGridView into the database MS SQL CE, but i can't, the changes are not saved to the database....

此代码(VB.net):

This the code (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

谢谢您的任何建议。

推荐答案

我认为您想在连接字符串中添加@

I think you want to add @ with your connection string

SqlConnection con;

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

这篇关于如何将更改从DataGridView保存到数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆