Dataadapter with DataGridView - Sql Server

The SqlDataAdapter serves as the intermediary between the Dataset and the Data Source, facilitating seamless communication with the assistance of the SqlConnection Object. It is important to note that the SqlConnection Object itself does not possess any information regarding the data it retrieves. Additionally, the Dataset lacks knowledge about the specific Data Source from which the data originates. Consequently, the SqlDataAdapter assumes the crucial role of managing the communication between these two distinct objects.

To complete this task successfully, the SqlDataAdapter utilizes the TableMapping Collections. These collections play a vital role in establishing the necessary connections and mappings between the Dataset and the Data Source. By utilizing the TableMapping Collections, the SqlDataAdapter ensures that the retrieved data is accurately and appropriately synchronized between the Dataset and the Data Source.

SqlDataAdapter object

The InsertCommand, UpdateCommand, and DeleteCommand properties of the SqlDataAdapter object are responsible for updating the database with any modifications made to a DataSet object. These modifications can include inserting new records, updating existing records, and deleting records.

Full Source VB.NET
Imports System.Data.SqlClient Public Class Form1 Dim connetionString As String Dim connection As SqlConnection Dim adapter As SqlDataAdapter Dim cmdBuilder As SqlCommandBuilder Dim ds As New DataSet Dim changes As DataSet Dim sql As String Dim i As Int32 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" connection = New SqlConnection(connetionString) Sql = "select * from Product" Try connection.Open() adapter = New SqlDataAdapter(Sql, connection) adapter.Fill(ds) connection.Close() DataGridView1.Data Source= ds.Tables(0) Catch ex As Exception MsgBox(ex.ToString) End Try End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Try cmdBuilder = New SqlCommandBuilder(adapter) changes = ds.GetChanges() If changes IsNot Nothing Then adapter.Update(changes) End If MsgBox("Changes Done") Catch ex As Exception MsgBox(ex.ToString) End Try End Sub End Class

Conclusion

The SqlDataAdapter plays a key role in mediating the communication between the Dataset and the Data Source, using the SqlConnection Object and the TableMapping Collections to facilitate efficient and accurate data retrieval and synchronization.