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
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.
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ìm kiếm theo từ khóa liên quan:
kĩ thuật lập trình công nghệ thông tin lập trình ngôn ngữ lập trình C Shark C# sybex - c.sharp database programming Understanding the SqlConnection ClassGợi ý tài liệu liên quan:
-
52 trang 411 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 291 0 0 -
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 286 0 0 -
74 trang 276 0 0
-
96 trang 275 0 0
-
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 265 1 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 261 0 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 256 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 251 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 245 0 0