O'Reilly logo

Programming Microsoft® ADO.NET 2.0 Core Reference, 2nd Edition by David Sceppa

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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;" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required