Creating SqlConnection Objects

There are two ways to create SqlConnection objects at run time. You can simply create an uninitialized SqlConnection object using the parameterless constructor, or you can use the constructor that accepts a connection string as shown here:

Visual Basic

Dim strConn As String
strConn = "Data Source=.\SQLExpress;" & _
          "Initial Catalog=Northwind;Integrated Security=True;"
Dim cn As New SqlConnection(strConn)

Visual C#

string strConn;
strConn = @"Data Source=.\SQLExpress;" +
          "Initial Catalog=Northwind;Integrated Security=True;";
SqlConnection cn = new SqlConnection(strConn);

If you haven’t used ADO.NET or a similar data access technology before, you might look at this code and wonder what a connection string is. For now, a ...

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.