One, authentication:
There are two types of SQL Server database connection authentication: Windows authentication and SQL Server Authentication
Windows authentication: A user connection is validated using the security subsystem of Windows. (personally think that the Windows operating system to help us verify user security, the default Windows administrator for the database administrator, you can do everything , but also can set their own database administrator)
SQL Server Authentication: a user name and password are required, and the default user name is SA, but can be changed.
Second, database connection:
1. Import the Msado15.dll file (found in the C drive)
Import in a header file or in a stdafx.h file, you do not need to import the header file of the appropriate class (this is different from the word component)
2. Creating connection objects and Recordset objects
_connectionptr Pconn;
Pconn.createinstance ("ADODB. Connection ");//strings are not case-sensitive after
_RecordsetPtr precordset;
Precordset.createinstace ("Adodb.connection");
Third, the connection string (all connection strings are case insensitive, and the keys and values are not distinguished):
Provider=sqloledb.1//The key is used to indicate the database type that provides the database service, and the value is not the same for different databases. Other types please refer to: http://www.w3school.com.cn/ado/prop_conn_provider.asp
Integrated security/trusted=true[yes,no,false,sspi]//when the key is yes,ture or SSPI means using Windows login mode, the UID and PWD are not required at this time. Otherwise, the SQL login method defaults to False. SQL login must provide user ID and password.
Persist Security Info=false[true]//when False, use with the key above to improve connection security
Workstation ID=computername[(local)]//Indicates the workstation name where the database resides, the default value is the local computer name
Data source/server/address/addr/network Address= (local)//The key represents the name of the server, the five are the same, according to the circumstances use one
Initial catalog/database=databasename//The key represents the database name (the database collection makes up the server), the two are the same
user id/uid=username//login name when using SQL login
password/pwd=password//password when using SQL login
For other key values, please refer to: https://www.cnblogs.com/niuyjdz/p/7533439.html
Iv. four parameters of the open () function connected to the database:
Open the database with _connectionptr.open () for multiple access to the database
For one visit, you only need to use _COMMANDPTR or _recordsetptr connection.
_connectionptr.open (_bstr_t (CONNECTIONSTR), _bstr_t (username), _bstr_t (PWD), option)// after three parameters are not required, all have default values
Parameter 1: The connection string, must contain at least the server, database parameters, when using the object's ConnectionString property, the parameter can be "" empty, at this time do not have to convert to _bstr_t type, otherwise it must be converted to that type;
Parameter 2, Parameter 3: When logging in SQL Sever, the required user name and password, if provided in Parameter 1, can be empty.
Parameter 3: Indicates synchronous or asynchronous return. Adconnectunspecified indicates that the connection is returned, that is, synchronous, Adasyncconnect is returned asynchronously, and is returned without a connection. The default value is synchronization.
Execute method for executing SQL statements (only _connectionptr and _commandptr have this method)
_RecordsetPtr Execute (_bstr_t cmdtext,_variant_t* recordeffected,options)
Return value: Is a recordset that is useful.
Parameter 1:sql statement, must be converted into _bstr_t format
Parameter 2: Returns a pointer to the number of rows affected by the statement, which can be omitted
Parameter 3: Indicates how to interpret parameter 1, generally adcmdtext, can be omitted, the default value is also the value.
Vi. closing the Database close ():
After the three objects have been exhausted, the close () function needs to be called off, and release () is called to free the resource.
You can also call release (), which is the release () that releases the interface.
It can also not be called, because it is a smart pointer that is automatically freed when the smart pointer is refactored.
Http://www.cnblogs.com/jeffry/p/5827849.html
ADO database connection string (SQL Server database)