Imports System.Data.SqlClient Public Class Form1 Inherits System.Windows.Forms.Form ****************************** DG e' una datagridwiev ****************************** Private da As SqlDataAdapter Private ds As DataSet Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load ' Create a Connection object Dim dbConn As New SqlConnection("server=(local); database=pubs; integrated security=true") ' Create the data adapter object pointing to the authors table da = New SqlDataAdapter( _ "SELECT au_id, au_lname, au_fname, phone FROM authors", dbConn) ' Fill the DataSet ds = New DataSet("Authors") da.Fill(ds) ' Display the records in a DataGrid component dg.DataSource = ds.Tables(0) End Sub Private Sub Form1_Closing(ByVal sender As Object, _ ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing ' Message box to prompt the save request If (MessageBox.Show("Do you want save the changes?", _ "Update", _ MessageBoxButtons.YesNo) = DialogResult.Yes) Then Try ' Create the insert, delete and update statements automatically Dim cb As New SqlCommandBuilder(da) ' Retrieve just the changed rows Dim dsChanges As DataSet = ds.GetChanges() If Not dsChanges Is Nothing Then ' Update the database da.Update(dsChanges) ' Accept the changes within the DataSet ds.AcceptChanges() End If Catch ex As Exception ' Error occurs, show the message MessageBox.Show(ex.Message) End Try End If End Sub |