The connection object can be used to connect to a database to manage database transactions.
1. Create a connection object
Four Database Connection Methods:
Oledb: System. Data. oledb. oledbconnection
SQL: System. Data. sqlcilent. sqlconnection
ODBC: System. Data. ODBC. odbcconnection
Oracle: System. Data. oracleclient. oracleconnection
Let's take a look at an instance created by oledbconnection. There are two methods in total.
First, create an uninitialized oledbconnection object and use connectionstring to initialize it.
Create a connection object
String strconn;
Strconn= "Provider = sqloledb; Data Source = {local) \ netsdk; initial catalog = northwind; trusted_connection = yes;";
Oledbconnection CN;
CN= NewOledbconnection ();
CN. connectionstring=Strconn;
Type 2: Use the constructor to initialize the oledbconnection object. create a connection object
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> string strconn ;< br> strconn = " provider = sqloledb; data Source = {local) \ netsdk; initial catalog = northwind; trusted_connection = yes; " ;< br> oledbconnection CN ;< br> CN = New oledbconnection (strconn) ;
2. connection string
Several common parameters used to connect strings:
Provider: this property is used to set or return to the connection providedProgramIs only used for oledbconnection objects.
Connection timeout or connect Timeout: Wait for the connection time (in seconds) to connect to the server before the attempt is aborted and an exception occurs ). The default value is 15 seconds.
Initail catalog: name of the database.
Data source: the name of the SQL server used when the connection is opened, or the file name of the Microsoft Access database.
Password: the logon password of the SQL Server account.
User ID: The Logon account of SQL Server.
Integrated Security or trusted connection: this parameter determines whether the connection is a secure connection. Possible values include true, false, and sspi (sspi is synonymous with true ).
Persist Security info: when it is set to false, if the connection is opened or has been opened, the security sensitive information (such as the password) will not be returned as part of the connection. Setting the attribute value to true may pose security risks. False is the default value.
3. Open and Close the connection
◆OpenMethod: Open the connection.
◆CloseMethod: Close the connection.
Note: If a connection pool is used, closing the connection object will not actually close the connection to the data source.
If you only use dataadapter, you do not need to explicitly open or close the connection. When you call these object methods (fill method, update method), the system automatically checks whether the connection is enabled.
I am so tired -_-!!