ADO.NET, Part 1
Pages: 1, 2
Connecting to a SQL Server Database
To read and write information to and from a SQL Server database, it is necessary first to establish a connection to the database. This is done with the SqlConnection object, found in the System.Data.SqlClient namespace. Here's an example:
' Open a database connection.Dim strConnection As String = _"Data Source=localhost;Initial Catalog=Northwind;" _& "Integrated Security=True"Dim cn As SqlConnection = New SqlConnection(strConnection)cn.Open( )
This code fragment instantiates an object of type SqlConnection, passing its constructor a connection string. Calling the SqlConnection object's Open method opens the connection. A connection must be open for data to be read or written, or for commands to be executed. When you're finished accessing the database, use the Close method to close the connection:
' Close the database connection.cn.Close( )
The connection string argument to the SqlConnection class's constructor provides information that allows the SqlConnection object to find the SQL Server database. The connection string shown in the earlier code fragment indicates that the database is located on the same machine that is running the code snippet (Data Source=localhost), that the database name is Northwind (Initial Catalog=Northwind), and that the user ID that should be used for logging in to SQL Server is the current Windows login account (Integrated Security=True). Table 8-1 shows the valid SQL Server connection string settings.
|
Setting |
Default Value |
Description |
|---|---|---|
|
|
|
Synonym for |
|
|
|
Synonym for |
|
|
|
The name of the client application. If provided, SQL Server uses this name in its |
|
|
|
Synonym for |
|
|
15 |
Synonym for |
|
|
15 |
The number of seconds to wait for a login response from SQL Server. If no response is received during this period, an SqlException exception is thrown. This setting corresponds to the SqlConnection object's ConnectionTimeout property. |
|
|
|
The language to use for this session with SQL Server. The value of this setting must match one of the entries in either the "name" column or the "alias" column of the "master.dbo.syslanguages" system table. If this setting is not specified, SQL Server uses either its system default language or a user-specific default language, depending on its configuration. The language setting affects the way dates are displayed and may affect the way SQL Server messages are displayed. Search for "SQL Server Language Support" in SQL Server Books Online for more information. |
|
|
|
The name or network address of the computer on which SQL Server is located. This setting corresponds to the SqlConnection object's DataSource property. |
|
|
|
Synonym for |
|
|
|
The name of the database to use within SQL Server. This setting corresponds to the SqlConnection object's Database property. |
|
|
|
The full pathname of the primary file of an attachable database. If this setting is specified, the Search for "Attaching and Detaching Databases" in SQL Server Books Online for more information.
|
|
` |
Indicates whether to use NT security for authentication. A value of ` Search for "How SQL Server Implements Security" in SQL Server Books Online for more information. |
|
|
|
` |
Synonym for |
|
|
|
Synonym for |
|
|
` |
The name of the .dll that manages network communications with SQL Server. The default value, ` Search for "Communication Components" and "Net-Libraries and Network Protocols" in SQL Server Books Online for more information. |
|
|
|
The SQL Server login password for the user specified in the |
|
|
` |
Specifies whether SqlConnection object properties can return security-sensitive information while a connection is open. Before a connection is opened, its security-sensitive properties return whatever was placed in them. After a connection is opened, properties return security-sensitive information only if the For example, if |
|
|
|
Synonym for |
|
|
|
Synonym for |
|
|
` |
Synonym for |
|
|
|
The SQL Server login account to use for authentication. |
|
|
the client computer name |
The name of the computer that is connecting to SQL Server. |
SQL Server AuthenticationBefore a process can access data that is located in a SQL Server database, it must log in to SQL Server. The SqlConnection object communicates with SQL Server and performs this login based on information provided in the connection string. Logging in requires authentication. Authentication means proving to SQL Server that the process is acting on behalf of a user who is authorized to access SQL Server data. SQL Server recognizes two methods of authentication:
To use SQL Server Authentication:
"Data Source=SomeMachine; Initial Catalog=Northwind; User ID=sa; Password=" To use Integrated Windows Authentication:
"Data Source= SomeMachine; Initial Catalog=Northwind; Integrated Security=True" When using Integrated Windows Authentication, it is necessary to know what Windows login account a process will run under and to set up appropriate rights for that login account in SQL Server Enterprise Manager. A program running on a local machine generally runs under the login account of the user that started the program. A component running in Microsoft Transaction Server (MTS) or COM+ runs under a login account specified in the MTS or COM+ Explorer. Code that is embedded in an ASP.NET web page runs under a login account specified in Internet Information Server (IIS). Consult the documentation for these products for information on specifying the login account under which components run. Consult the SQL Server Books Online for information on setting up SQL Server login accounts and on specifying account privileges. |
Next week, check out the next excerpt installment that shows you how to connect to an OLE DB data source using ADO.NET.
View catalog information for Programming Visual Basic .NET
Return to the .NET DevCenter.

