Understanding the SqlConnection Class You use an object of the SqlConnection class to connect to a SQL Server database, and this object handles the communication between the database and
Trang 1Understanding the SqlConnection Class
You use an object of the SqlConnection class to connect to a SQL Server database, and this object handles the communication between the database and your C# program
Note Although the SqlConnection class is specific to SQL Server, many of the properties, methods, and events in this class are the same as those for the OleDbConnection and OdbcConnection classes If a property or method is specific to SqlConnection,
it says so in the Description column of the tables shown in this section You can look up the exact properties, methods, and events for a specific class using the NET
Programming with ADO.NET."
Table 7.1: SqlConnection PROPERTIES
PROPERTY TYPE DESCRIPTION
establish a connection to a database The default is
15 seconds
database to be used once the connection to the database is made)
communicate with SQL Server This property applies only to the SqlConnection class The default is 8,192 bytes
Closed, Connecting, Executing, Fetching, or Open These states are covered later in the "Getting the State of a Connection" section
is connected to SQL Server This property applies only to the SqlConnection class
Trang 2Table 7.2 shows some of the SqlConnection methods
Table 7.2: SqlConnection METHODS
METHOD RETURN
TYPE
DESCRIPTION
BeginTransaction() SqlTransaction Overloaded Begins a database transaction
connection
CreateCommand() SqlCommand Creates and returns a command object
settings specified by the ConnectionString property
You can use events to allow one object to notify another object that something has
occurred For example, when you click a mouse button in a Windows application, an
Table 7.3: SqlConnection EVENTS
EVENT EVENT HANDLER DESCRIPTION
changed
InfoMessage SqlInfoMessageEventHandler Fires when the database returns a warning or
information message
You'll learn how to use some of these properties, methods, and events in the following sections