Creating and Using SqlDataAdapter Objects

You now know what a SqlDataAdapter is and what it can do for you, so let’s examine how to create and use one.

Creating a SqlDataAdapter

When you create a SqlDataAdapter, you generally want to set its SelectCommand property to a valid SqlCommand object. The following code sample sets the SelectCommand for a new SqlDataAdapter:

Visual Basic

Dim strConn, strSQL As String
strConn = "Data Source=.\SQLExpress;" & _
          "Initial Catalog=Northwind;Integrated Security=True;"
strSQL = "SELECT CustomerID, CompanyName FROM Customers"
Dim cn As New SqlConnection(strConn)
Dim cmd As New SqlCommand(strSQL, cn)
Dim da As New SqlDataAdapter()
da.SelectCommand = cmd

Visual C#

string strConn, strSQL; strConn = @"Data Source=.\SQLExpress;" ...

Get Programming Microsoft® ADO.NET 2.0 Core Reference, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.