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 your C# program.
Nội dung Text: Understanding the SqlConnection Class
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 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
online reference. You saw how to do that in Chapter 1, "Introduction to Database
Programming with ADO.NET."
Table 7.1 shows some of the SqlConnection properties.
Table 7.1: SqlConnection PROPERTIES
PROPERTY TYPE DESCRIPTION
ConnectionString string Gets or sets the string used to open a database.
ConnectionTimeout int Gets the number of seconds to wait while trying to
establish a connection to a database. The default is
Database string Gets the name of the current database (or the
database to be used once the connection to the
database is made).
DataSource string Gets the name of the database server.
PacketSize int Gets the size (in bytes) of network packets used to
communicate with SQL Server. This property
applies only to the SqlConnection class. The
default is 8,192 bytes.
ServerVersion string Gets a string containing the version of SQL Server.
State ConnectionState Gets the current state of the connection: Broken,
Closed, Connecting, Executing, Fetching, or Open.
These states are covered later in the "Getting the
State of a Connection" section.
WorkstationId string Gets a string that identifies the client computer that
is connected to SQL Server. This property applies
only to the SqlConnection class.
Table 7.2 shows some of the SqlConnection methods.
Table 7.2: SqlConnection METHODS
METHOD RETURN DESCRIPTION
BeginTransaction() SqlTransaction Overloaded. Begins a database transaction.
ChangeDatabase() void Changes the current database for an open
Close() void Closes the connection to the database.
CreateCommand() SqlCommand Creates and returns a command object.
Open() void Opens a database connection with the property
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
event occurs, or is fired. Table 7.3 shows some of the SqlConnection events. You'll learn
how to use these events later in the "Using Connection Events" section.
Table 7.3: SqlConnection EVENTS
EVENT EVENT HANDLER DESCRIPTION
StateChange StateChangeEventHandler Fires when the state of the connection is
InfoMessage SqlInfoMessageEventHandler Fires when the database returns a warning or
You'll learn how to use some of these properties, methods, and events in the following