Datagridview save changes to Database vb.net

后端 未结 3 804
遥遥无期
遥遥无期 2020-12-11 22:47

Hello i\'ve a databse that i load to Datagridview in vb.net application. it loads fine, however when i try to save the date it doesn\'t work. here is the code



        
相关标签:
3条回答
  • 2020-12-11 22:56

    My VB.NET code for the 4 type of databases (update information from DataGridView to Database)

    Private Sub sqldb_savedata()
        Dim connectionString As String = "Server='" & sql_server & "';Database='" & sql_database & "';User Id='" & sql_user & "';Password='" & sql_pass & "'"
        Dim sqlCon = New SqlConnection(connectionString)
        If (sqlCon.State = ConnectionState.Closed) Then sqlCon.Open()
        Dim SQLAdapter = New SqlDataAdapter("SELECT * FROM clinics", sqlCon)
        Dim SQLDataSet As New DataSet
        Dim myTable = DataGridViewClinic.DataSource
        Dim cmdbuilder As New SqlCommandBuilder(SQLAdapter)
        SQLAdapter.Update(myTable, "clinics")
        MsgBox("Updated!", MsgBoxStyle.OkOnly, "")
    End Sub
    
    Private Sub mysqldb_savedata()
        Dim connectionString As String = "Server='" & mysql_server & "';Database='" & mysql_database & "';User Id='" & mysql_user & "';Password='" & mysql_pass & "'"
        Dim sqlCon = New MySqlConnection(connectionString)
        If (sqlCon.State = ConnectionState.Closed) Then sqlCon.Open()
        Dim SQLAdapter = New MySqlDataAdapter("SELECT * FROM clinics", sqlCon)
        Dim SQLDataSet As New DataSet
        Dim myTable = DataGridViewClinic.DataSource
        Dim cmdbuilder As New MySqlCommandBuilder(SQLAdapter)
        SQLAdapter.Update(myTable, "clinics")
        MsgBox("Updated!", MsgBoxStyle.OkOnly, "")
    End Sub
    
    Private Sub firebirddb_savedata()
        Dim connectionString As String = "Database='" & firebird_server & "';User=SYSDBA;Password=masterkey;Dialect=3;ServerType=1"
        Dim sqlCon = New FirebirdSql.Data.FirebirdClient.FbConnection(connectionString)
        If (sqlCon.State = ConnectionState.Closed) Then sqlCon.Open()
        Dim SQLAdapter = New FirebirdSql.Data.FirebirdClient.FbDataAdapter("SELECT * FROM clinics", sqlCon)
        Dim SQLDataSet As New DataSet
        Dim myTable = DataGridViewClinic.DataSource
        Dim cmdbuilder As New FirebirdClient.FbCommandBuilder(SQLAdapter)
        SQLAdapter.Update(myTable, "clinics")
        MsgBox("Updated!", MsgBoxStyle.OkOnly, "")
    End Sub
    
     Private Sub localdb_savedata()
        DBconn = New SqlCeConnection("Data Source=Data Source=|DataDirectory|\Database.sdf")
        If (DBconn.State = ConnectionState.Closed) Then DBconn.Open()
        Dim SQLAdapter = New SqlCeDataAdapter("SELECT * FROM clinics", DBconn)
        Dim SQLDataSet As New DataSet
        Dim myTable = DataGridViewClinic.DataSource
        Dim cmdbuilder As New SqlCeCommandBuilder(SQLAdapter)
        SQLAdapter.Update(myTable, "clinics")
        MsgBox("Updated!", MsgBoxStyle.OkOnly, "")
    End Sub
    
    0 讨论(0)
  • 2020-12-11 23:02

    So you must define an InsertCommand for you DataAdapter

    Side-note: The line DSet.AcceptChanges() is redundant since the previous line Dadapter.Update will call AcceptChanges implicitely.

    You should use using-statement for anything implementing IDisposable like a Connection. That would call Dispose(which closes the connection) implicitely even in case of an exception.

    So replace:

    con.Open()
    Dadapter.Update(DSet, "Table1")
    DSet.AcceptChanges()
    con.Close()
    

    with

    Using con =  New OleDbConnection(myConString)
        con .Open()
        Dadapter.Update(DSet, "Table1")
    End Using
    
    0 讨论(0)
  • 2020-12-11 23:19

    You need to read back the dataset from the datagrid

    con.Open()
    DSet = DataGridView1.DataSource  '<<<<<<<<<<<<<<<<<<<<<<<
    Dadapter.Update(DSet, "Table1")
    DSet.AcceptChanges()
    con.Close()
    
    0 讨论(0)
提交回复
热议问题