asp.net2.0 Connection SQL Server database detailed

Source: Internet
Author: User
Tags constructor odbc oracleconnection oracle database

This article describes in detail how to use the connection object to connect to a database. For the different. NET data provider, ADO. NET uses different connection objects to connect to the database. These connection objects mask the specific implementation details, and provide a unified implementation method.

There are four types of connection: Sqlconnection,oledbconnection,odbcconnection and OracleConnection.

The object of the SqlConnection class connects to the SQL Server database; The object of the OracleConnection class connects to the Oracle database;

Objects in the OleDbConnection class connect to databases that support OLE DB, such as access, while objects of the OdbcConnection class connect to any database that supports ODBC. All communication with the database is ultimately done through the connection object.

SqlConnection class

Connection is used to "talk" to the database and is represented by classes such as SqlConnection for a particular provider. Although the SqlConnection class is for SQL Server, many of the properties, methods, and events of this class are similar to those of OleDbConnection and OdbcConnection. This chapter will focus on SqlConnection specific properties and methods, and other connection classes you can refer to the appropriate Help documentation.

Note: You need to import different namespaces using different connection objects. The OleDbConnection namespace is System.Data.OleDb. The SqlConnection namespace is System.Data.SqlClient. The OdbcConnection namespace is System.Data.Odbc. The OracleConnection namespace is System.Data.OracleClinet.

SqlConnection properties:

Property Description
ConnectionString The return type is string, gets or sets the string used to open the SQL Server database.
ConnectionTimeout The return type is int to get the time to wait before terminating the attempt and generating an error when attempting to establish a connection.
Database The return type is string, obtaining the name of the database to use when the current database or connection is opened.
DataSource The return type is string, obtaining the name of the SQL Server instance to which you want to connect.
State The return type is connectionstate, obtaining the current connection status: Broken, Closed, connecting, fetching, or open.
ServerVersion The return type is string, and gets a string containing the version of the SQL Server instance of the client connection.
PacketSize Gets the size, in bytes, of the network packet used to communicate with an instance of SQL Server. This property applies only to SqlConnection types

SqlConnection method:

Method Description
Close () The return type is void, and the connection to the database is turned off.
CreateCommand () The return type is SqlCommand, and a SqlCommand object associated with SqlConnection is created and returned.
Open () The return type is void, opening the database connection with the property specified by the connection string property

SqlConnection event:

Event Description
StateChange Occurs when the event state changes. (inherited from DbConnection.) )
InfoMessage Occurs when SQL Server returns a warning or informational message.

Tip: You can use events to let an object notify another object in some way of something. For example, we select the Start menu in the Windows system, and once you click the mouse, an event occurs that informs the operating system that the Start menu is displayed.

To connect to a SQL Server database using the SqlConnection object

We can generate a new SqlConnection object with the SqlConnection () constructor. This function is overloaded, that is, we can call different versions of the constructor. The constructor for SqlConnection () is shown in the following table:

Constructors Description
SqlConnection () Initializes a new instance of the SqlConnection class.
SqlConnection (String) Initializes a new instance of the SqlConnection class, given a string containing the connection string.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.