Danh mục

Understanding the SqlConnection Class

Số trang: 2      Loại file: pdf      Dung lượng: 20.24 KB      Lượt xem: 11      Lượt tải: 0    
Thư viện của tui

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

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 trích xuất từ tài liệu:
Understanding the SqlConnection ClassUnderstanding the SqlConnection ClassYou use an object of the SqlConnection class to connect to a SQL Server database, andthis 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 PROPERTIESPROPERTY TYPE DESCRIPTIONConnectionString 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 15 seconds.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 METHODSMETHOD RETURN DESCRIPTION TYPEBeginTransaction() SqlTransaction Overloaded. Begins a database transaction.ChangeDatabase() void Changes the current database for an open connection.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 hasoccurred. For example, when you click a mouse button in a Windows application, anevent occurs, or is fired. Table 7.3 shows some of the SqlConnection events. Youll learnhow to use these events later in the Using Connection Events section. Table 7.3: SqlConnection EVENTSEVENT EVENT HANDLER DESCRIPTIONStateChange StateChangeEventHandler Fires when the state of the connection is changed.InfoMessage SqlInfoMessageEventHandler Fires when the database returns a warning or information message.Youll learn how to use some of these properties, methods, and events in the followingsections.

Tài liệu được xem nhiều: