I. Features
In the ADO.NET object model, the Connection object represents a connection between the database. You can use the properties of the Connection object to specify the location of the user credentials and the data source.
Second, use introduction
1, basically
String strconn = "data source = 192.168.1.81; initial catalog = northwind; Persist security info = false; user ID = sa; password =;";
SqlConnection CN = New SqlConnection (STRCONN);
Cn.open ();
........
Cn.close ();
2, connect pool
The connection pool is used by default;
When you don't want to use: Join "OLE DB Service = -4" in the OLE DB connection string;
Add "pooling = false" in the SQL connection string;
3, relationship with other objects
I, create Commands
String strconn = ".........";
SqlConnection CN = New SqlConnection (STRCONN);
Cn.open ();
SQLCommand cmd = cn.createcommand ();
II, start a transaction
String strconn = ".........";
SqlConnection CN = New SqlConnection (STRCONN);
Cn.open ();
SQLTransaction TXN = cn.begintransaction ();
Third, the attribute method introduction
1, attribute
⒈Connectionstring: Connect the string, which contains the source database name and other parameters required to establish an initial connection. The default is an empty string. The attributes can only be set when Connection is not connected to the data source, and when connected to the data source, the attribute is read-only.
⒉ConnectionTIMEOUT: Wait for a connection to open time (in seconds). The default is 15 seconds.
⒊DATABASE: The name of the current database or the database to use after the connection is open. The default is an empty string.
⒋DataSource: The name of the SQL Server instance to connect. The default is an empty string.
⒌State: ConnectionState value bitmap. The default is closed.
Member name
Description
value
Broken is interrupted with the connection of the data source. This only happens after the connection is open. You can turn off the connection in this state and then turn it on.
16
The Closed connection is closed.
0
The Connecting connection object is connected to the data source.
2
The executing connection object is executing the command.
4
The fetching connection object is retrieving data.
8
The OPEN connection is open.
1
⒍SERVERVERSITION: The version of the SQL Server instance. The version of the format is ##. ##. ####, where the first two are the main version, the middle two is the secondary version, and the last four is the release version. The format of the string is Major.minor.build, where Major and Minor must be two digits, Build must be four digits.
⒎Packetsize: The size of the network packet (in bytes) The default value is 8192. If the application executes a bulk copy operation, or sending or receiving a large amount of text or image data, it can improve the efficiency than the data packet larger than the default size because it reduces the network read and write operations. If the application sends or receive a small amount of information, the packet can be set to 512 bytes, which is sufficient to cope with most data transfer operations. For most applications, it is best to use the default packet size. PacketSize can be a value within the 512 and 32767 bytes. If this value exceeds this range, it will produce an exception.
2, method
⒈BEGINTRANSACTION: Start data transactions
⒉ChangeDatabase: Change the current database on an open connection
⒊Close: Close connection
⒋createCommand: Create a SQLCommand for the current connection
⒌open: open connection
3, event
⒈INFMESSAGE: When SQL Server returns a warning or information message.
When SQL Server returns a message that is more than or less than 10, an InfMessage event is excited. The news between the severity between 11 and 20 causes an error, while a message that is more than 20 will cause a connection to close.
⒉StateChange: When the connection status changes. As long as the state value value of the Connection object changes, the STATECHANGE event of the object is triggered.