Tải bản đầy đủ (.pdf) (10 trang)

Microsoft SQL Server 2005 Developer’s Guide- P21 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (245.57 KB, 10 trang )

Chapter 6: Developing Database Applications with ADO.NET 179
When changes are made to the data contained in a DataTable object, the
ColumnChanging, ColumnChanged, RowChanging, and RowChanged events
are fired. When data is deleted from a DataTable object, the RowDeleting and
RowDeleted events are fired. New rows are added to a DataTable by calling the
DataTable’s NewRow method and passing it a DataRow object. The maximum
number of rows that can be stored in a DataTable is 16,777,216. The DataTable
is also used as a basis to create DataView objects.
DataColumn
The DataColumn class is located in the .NET Framework at System.Data.DataColumn.
The DataColumn class represents the schema of a column in a DataTable object. The
DataColumn class contains several properties that are used to define the type of data
contained in the DataColumn object. For example, the DataType property controls
the type of data that can be stored in the DataColumn object, the DataValue property
contains the DataColumn’s value, the AllowDBNull property specifies whether the
DataColumn can contain NULL values, the MaxLength property sets the maximum
length of a Text DataType, and the Table property specifies the DataTable object that
DataSet
DataTable
DataColumn
DataConstraint
DataRow
DataView
DataTable DataTable
DataColumn
DataConstraint
DataColumn
DataConstraint
DataRelationCollection
Figure 6-2 The DataSet architecture
180 Microsoft SQL Server 2005 Developer’s Guide


the DataColumn belongs to. DataColumns can be made to contain unique values by
associating a UniqueConstraint object with the DataColumn object. In addition, you
can relate a DataColumn object to another DataColumn object by creating
a DataRelation object and adding it to the DataSet’s DataRelationCollection.
DataRow
Found in the .NET Framework at System.Data.DataRow, the DataRow class represents
a row of data in the DataTable object. The DataRow class and the DataColumn class
represent the primary objects that make up the DataTable class. The DataRow object
is used to insert, update, and delete rows from a DataTable. Rows can be added to
a DataTable by either creating a new DataRow object using the NewRow method or
by Adding a DataRow object to the DataSet’s DataRowCollection. DataRow objects
are updated by simply changing the DataRow object’s DataValue property. You delete
a DataRow object by executing the DataRow object’s Delete method or by calling the
DataSet’s DataRowCollection object’s Remove method.
DataView
Found in the .NET Framework at System.Data.DataView, the DataView class offers
a customized view of a subset of rows in a DataTable object. Like the DataTable
object, DataView objects can be bound to both WinForm and WebForm controls.
The DataView classes’s RowFilter and Sort properties can allow the data presented
by the DataView to be displayed in a different order than the data presented by the
base DataTable object. Like the DataTable object, the data contained in a DataView
object is updatable. You can add new rows by using the AddNew method, and you
can delete rows by using the Delete method.
DataViewManager
The DataViewManager class is located in the .NET Framework at SystemData.Data-
ViewManager. The DataViewManager class is a bit different than the other classes in
the System.Data namespace. Essentially, the DataViewManager class tracks the Data-
ViewSetting objects for each DataTable in the DataSet in its DataViewSettingsCollec-
tion. The DataViewSettingsCollection is a group of DataViewSetting objects where
each DataViewSetting object contains properties like the RowFilter, RowStateFilter,

and Sort that define each DataView object.
Chapter 6: Developing Database Applications with ADO.NET 181
DataRelation
The DataRelation class is located in the .NET Framework at System.Data.
DataRelation. The DataRelation class is used to represent parent-child relationships
between two DataTable objects contained in a DataSet. For example, you could
create a DataRelation object between an OrderID DataColumn in an Order Header
table to the corresponding OrderID DataColumn in an Order Detail table. The basic
function of the DataRelation object is to facilitate navigation and data retrieval
from related DataTables. In order to create a relationship between two DataTable
objects, the two DataTables must contain DataColumn objects that have matching
attributes. When a DataRelation is first created, the .NET Framework checks to
make sure that the relationship is valid and then adds the DataRelation object to the
DataRelationCollection, which tracks all of the data relations for the DataSet. The
DataRelation class supports cascading changes from the parent table to the child
table, and this is controlled through the ForeignKeyConstraint class.
Constraint
Found in the .NET Framework at System.Data.Constraint, the Constraint class
represents a set of data integrity rules that can be applied to a DataColumn object.
There is no base constructor for the Constraint class. Instead, constraint objects are
created using either the ForeignKeyConstraint constructor or the UniqueConstraint
constructor.
ForeignKeyConstraint
The ForeignKeyConstraint class is located in the .NET Framework at SystemData.
ForeignKeyConstraint. The ForeignKeyConstraint class governs how changes in
a parent table affect rows in the child table when a DataRelation exists between the
two tables. For example, when you delete a value that is used in one or more related
tables, a ForeignKeyConstraint class’s DeleteRule property determines whether the
values in the related tables are also deleted. Deleting a value from the parent table
can delete the child rows; set the values in the child table’s rows to null values; set

the values in the child table’s rows to default values; or throw an exception.
UniqueConstraint
The UniqueConstraint class is located in the .NET Framework at SystemData.
UniqueConstraint. The UniqueConstraint class ensures that all values entered into
a DataColumn object have a unique value.
182 Microsoft SQL Server 2005 Developer’s Guide
DataException
Found in the .NET Framework at System.Data.DataException, the DataException
class represents an error that is thrown by one of the System.Data classes. For
example, code that violates a UniqueConstraint on a DataColumn by attempting to
add a duplicate value to the DataColumn will cause a DataException object to be
created and added to the DataExceptionCollection. You can use the DataException
objects to report error conditions in your ADO.NET applications.
Using the .NET Framework Data Provider
for SQL Server
The .NET Framework Data Provider for SQL Server will give you a significant
performance boost if your application only needs to connect to SQL Server and it
doesn’t need to connect to any other database systems. When accessing SQL Server
databases, the .NET Framework Data Provider for SQL Server is more efficient than
the .NET Framework Data Provider for OLE DB or ODBC because it communicates
between the client application and the SQL Server system using SQL Server’s native
TDS (Tabular Data Stream) protocol. The System.Data.SqlClient namespace also
includes a new signaling solution called Query Notifications. Query Notifications
can be implemented using the SqlDependency object discussed later in this section.
Adding the System.Data.SqlClient Namespace
While using the visual connection components that are provided by the Visual
Studio.NET design environment makes it pretty easy to create an initial connection
to a SQL Server system, they also tend to clutter up the design environment. After
your first couple of connections using them, you’ll probably be ready to forgo the
visual components in the Data Toolbox and establish your database connection

exclusively using code. Using the ADO.NET objects in code requires only a couple
of extra steps. In return you get more screen real estate for the Designer window and
more control over exactly when and how the SqlConnection objects get created.
Before you can use the .NET Framework Data Provider for SQL Server in your
code, you must first specify an import directive for the System.Data.SqlClient
namespace in your project. This step isn’t required when using the visual data
components, but it is required in order to use the objects contained in the System.
Data.SqlClient namespace with code. The System.Data.SqlClient namespace contains
all of the related SQL Server connection and data access classes. To add an import
Chapter 6: Developing Database Applications with ADO.NET 183
directive for the System.Data.SQLClient to a VB.NET project, you would add the
following code to the declaration section of your source file:
Imports System.Data.SqlClient
Using the SqlConnection Object
After adding an import directive to your code, you’re ready to begin using the different
classes contained in the System.Data.SqlClient namespace. The most essential of those
classes is the SqlConnection class. As its name implies, the System.Data.SqlClient
SqlConnection class is used to connect to a SQL Server database. You can use several
different techniques to connect the System.Data.SqlClient namespace to SQL Server.
The technique that’s probably most familiar to developers with previous ADO
experience is setting the ConnectionString property with a valid connection string and
then invoking the Open method. The following example illustrates how to make a SQL
Server connection by setting the System.Data.SqlClient namespace’s ConnectionString
Property:
Private Sub SQLConnectString(ByVal sServer, ByVal sUser, ByVal sPassword)
Dim cn As New SqlConnection()
' Set the connection string
cn.ConnectionString = "SERVER=" & sServer & _
";UID=" & sUser & ";PWD=" & sPassword
Try

' Open the connection
cn.Open()
Catch ex As Exception
' Display any error messages
MessageBox.Show("Connection error: :" & ex.ToString())
End Try
' Close the connection
cn.Close()
End Sub
In this case string variables containing the name of the SQL Server system
to connect to along with the user ID and password are passed into the top of the
routine. Next, a new instance of the System.Data.SqlClient Connection object
named cn is created. Then the ConnectionString property of the System.Data.
SqlClient Connection object is assigned the .NET Framework Data Provider for
SQL Server connection string. This connection string uses the SERVER keyword
to identify the SQL Server system that it will be connected to. The UID and PWD
184 Microsoft SQL Server 2005 Developer’s Guide
keywords provide the authentication values required to log in to SQL Server if
you are connecting using mixed security. A UID and a PWD are not required
in the connection string if you are connecting using a trusted connection, as
discussed later in this chapter. A complete list of the valid the .NET Framework
Data Provider for SQL Server connection string keywords is presented in the next
section, “The .NET Framework Data Provider for SQL Server Connection String
Keywords.” After the ConnectionString property has been assigned the appropriate
connection string, a Try-Catch block is used to execute the cn Connection object’s
Open method. After the Open method completes, a connection to the SQL Server
system identified in the connection string is initiated. If there was an error with
the connection string or the specified SQL Server system is not available, the code
in the Catch block will be executed and a message box will be displayed showing
the error information. After a successful connection has been established, the

Connection object is closed using the Close method.
NOTE
Explicitly using the Close method is very important in ADO.NET to ensure that the resources
allocated by the Connection object are released when they are no longer needed. In .NET
applications the Connection object is not necessarily destroyed when it goes out of scope. Executing
either the Close or Dispose method is required to make sure that the connection resources are
released. The Close method closes the current connection, but the underlying .NET managed
resources used for connection pooling will remain available. Close can be called multiple times—
even when the connection is already closed—without raising an error. The Dispose method can
release all managed and unmanaged resources used by a connection, and it can only be called for
an active connection.
The .NET Framework Data Provider for SQL Server
Connection String Keywords
The SQL Server .Net Data Provider connection string is much like the OLE
DB connection string that was used by ADO. However, unlike in the OLE DB
connection string, the login values contained in the connection string are not
returned to the application unless you explicitly tell the provider to do so via the
Persist Security Info connection string keyword. In addition, the SQL Server .NET
Data Provider also supports a few new keywords. Table 6-1 lists all the SQL Server
.NET Data Provider–specific keywords supported by the SQLConnection object’s
ConnectionString property.
Chapter 6: Developing Database Applications with ADO.NET 185
Keyword Description
Application Name Identifies the current application.
AttachDBFilename -or-
Extended properties -or-
Initial File Name
Identifies the full path and name of a file that will be attached as a SQL Server database.
This keyword must be used in conjunction with the Database keyword.
Connect Timeout -or-

Connection Timeout
Specifies the length of time in seconds to wait before terminating a connection attempt.
The default is 15.
Connection Lifetime Specifies the length of time in seconds to wait before destroying a connection returned to
the connection pool. This keyword is used for load balancing in a cluster. The default is 0.
Connection Reset Specifies that a connection will be reset when it is returned from the connection pool. The
default is ‘true’.
Current Language Specifies the SQL Server language name to be used for this connection.
Data Source -or-
Server -or-
Address -or-
Addr -or-
Network Address
Identifies the name or network address of a SQL Server instance to connect to.
Enlist Determines whether the current thread will be enlisted as part of the current transaction
context. The default value is ‘true’.
Encrypt Determines whether SSL will be used to encrypt the data stream sent between the
application and SQL Server. The default value is ‘false’.
Initial Catalog -or-
Database
The SQL Server target database name.
Integrated Security -or-
Trusted_Connection
Uses a value of ‘true’ or ‘SSPI’ to indicate where Windows authentication is to be used
to connect to the database and a value of ‘false’ to indicate that mixed or SQL Server
authentication should be used.
Max Pool Size The default value is 100.
Min Pool Size The default value is 0.
Network Library -or-
Net

Specifies the network library DLL to be used. Supported values include ‘dbnmpntw’
(Named Pipes), ‘dbmsrpcn’ (Multiprotocol), ‘dbmsadsn’ (AppleTalk), ‘dbmsgnet’ (VIA),
‘dbmsipcn’ (Shared Memory), ‘dbmsspxn’ (IPX/SPX), and ‘dbmssocn’ (TCP/IP). The
default value is ‘dbmssocn’. The value used by this keyword should not include the path of
the .dll file extension.
Packet Size Used to alter the network packet size. The default packet size is 8192.
Password -or-
Pwd
The password associated with the login ID (used for SQL Server authentication).
Table 6-1 SQL Server .NET Data Provider Connection String Keywords
186 Microsoft SQL Server 2005 Developer’s Guide
NOTE
Some of the keywords displayed contain spaces. If so, those spaces are required. In addition, for
those items that have multiple keywords designated by the -or-, you can use any of the keywords.
The .NET Framework Data Provider for SQL Server connection string keywords are not case- sensitive.
However, it’s good programming practice to be consistent in your usage of keyword case in all of
your applications.
Opening a Trusted Connection
The previous example illustrated how to establish a SQL Server connection using
a connection string that specified the UID and PWD keywords along with an associated
SQL Server login. (This is also known as using Mixed Security.) However, because
this incorporates the actual user ID and password into your code, this certainly isn’t the
most secure way to authenticate your connection to the SQL Server system.
Using Windows Security, also known as Integrated Security, provides for a more
secure connection because the same values used for the client’s Windows NT/2000/
NET login are also used for SQL Server authentication—there’s no need to specify
the user ID or the password from the application. In addition, Integrated Security
can make administration easier by eliminating the need to create a set of SQL Server
login IDs that are separate and must be maintained independently from the Windows
NT/2000/NET login information. The following example illustrates how to use

VB.NET to make a trusted connection to SQL Server using the .NET Framework
Data Provider for SQL Server:
Keyword Description
Persist Security Info Specifies whether security-sensitive information such as login information is returned to
the application after a successful connection. The default value is ‘false’.
Pooling The default value is ‘true’.
User ID -or-
UID
The login ID for the data source (used for SQL Server authentication).
Workstation ID Identifies the client workstation.
Table 6-1 SQL Server .NET Data Provider Connection String Keywords (Continued)
Chapter 6: Developing Database Applications with ADO.NET 187
Private Sub SQLConnectSSPI(ByVal sServer As String)
' Create the connection object
Dim cn As New SqlConnection(
"SERVER=" & sServer & _
";INTEGRATED SECURITY=True")
Try
' Open the connection
cn.Open()
Catch ex As Exception
' Display any error messages
MessageBox.Show(
"Connection error: :" & ex.ToString())
End Try
' Close the connection
cn.Close()
End Sub
In the beginning of this subroutine, you can see where the server name is passed
in as a string value. Next, an instance of the SqlConnection object is created and the

ConnectionString property is assigned as one of the arguments of the constructor.
Like the previous example, the connection string uses the SERVER keyword to
specify the SQL Server instance to connect to, and the INTEGRATED SECURITY
keyword is set to true, indicating that the SQL Server authentication will be performed
using Integrated Security rather than by passing in a login ID and password as part
of the connection string.
After an instance of the SqlConnection object named cn has been instantiated,
a Try-Catch block is used to execute the Open method. Again, if the Open method
fails, then the code in the Catch block will be executed and a message box will
be displayed showing the specific error message. After the connection has been
established, it is immediately closed using the Connection object’s Close method.
Using Connection Pooling
Connection pooling is an important scalability feature that’s particularly significant
to n-tier-style web applications, which may need to quickly support hundreds of
simultaneous connections. Each open connection to SQL Server requires system
overhead and management. And initially establishing the connection is the highest-
overhead activity associated with each connection. Connection pooling makes the
overall connection process more efficient by sharing a group or pool of connections
between incoming users. Rather than immediately opening individual connections
for each user, with connection pooling all connections that share exactly the same
188 Microsoft SQL Server 2005 Developer’s Guide
connection characteristics share the same connection, reducing the total number
of new connections that must be established and maintained by SQL Server. To
further improve efficiency, open connections are not immediately closed when
a given client disconnects from the server. Rather, the connection is left open for
a short period of time (determined by the Connection Lifetime keyword that’s used
in the SqlConnection object’s ConnectionString property). This makes it possible
for the connection to be immediately available for any new clients that can share
the same connection characteristics, thereby avoiding the overhead associated with
establishing a new connection.

Better still, the .NET Framework Data Provider for SQL Server automatically
performs connection pooling without requiring any special setup. When a
connection is opened, a connection pool is created based on the values used in the
ConnectionString property of the SqlConnection object. Each connection pool is
associated with a unique connection string. When a new connection is opened, the
SqlConnection object checks to see if the value in the ConnectionString property
matches the connection string used for an existing pool. If the string matches, the
new connection is added to the existing pool. Otherwise, a new pool is created. The
SqlConnection object will not destroy a connection pool until the application ends.
The following VB.NET example illustrates creating two different connections that
are both added to the same connection pool:
Private Sub SQLConnectPool(ByVal sServer As String)
' Create the first connection object
Dim cn As New SqlConnection("SERVER=" & sServer & _
";INTEGRATED SECURITY=True")
' Create the second identical connection object
Dim cn2 As New SqlConnection("SERVER=" & sServer & _
";INTEGRATED SECURITY=True”)
Try
' Open the connections
cn.Open()
cn2.Open()
Catch ex As Exception
' Display any error messages
MessageBox.Show("Connection error: :" & ex.ToString())
End Try
' Close the connections
cn.Close()
cn2.Close()
End Sub

×