Article From:https://www.cnblogs.com/zhuluqing/p/9059932.html

First, authentication:

sql serverThere are two kinds of database connection authentication: Windows authentication and SQL Server authentication.

windowsValidation: use Windows’s security subsystem to validate user connections. (personal view is that the windows operating system helps us verify user security.The default Windows administrator is the administrator of the database and can do all the operations.,But you can also set up your own database administrator.

sql serverValidation: user name and password are required. The default user name is SA, but it can be changed.

Two, database connection:

1.Import the msado15.dll file (look up in the C disk)

You can import it in header files or stdafx.h files without importing the header files of corresponding classes (this is different from word components).

2.Create connection objects and recordset objects

_ConnectionPtr pConn;

pConn.CreateInstance(“ADODB.Connection”);//The following string does not distinguish case

_RecordSetPtr pRecordSet;

pRecordSet.CreateInstace(“adodb.connection”);

Three. The connection string (All connection strings are not case sensitive, and keys and values are indistinguishes.):

Provider=SQLOLEDB.1//This key is used to indicate the type of database that provides database services, and the values of different databases are different. For 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, the windows login mode is used. There is no need to provide uid and PWD at this time. Otherwise, for SQL landing, the default is False. The SQL landing must provide User ID and password.

Persist Security Info=False[True]//When False is used, it can improve the connection security with the above key.

Workstation ID=computername[(local)] //Represents the name of the workstation in which the database is located, and the default value is the local computer name.

Data Source/Server/Address/Addr/Network Address=(local)//This key represents the name of the server. The five are the same.

Initial Catalog/Database=databasename//This key represents the database name (database collection is composed of servers), and the two are the same.

User ID/Uid=username//The name of the landing at SQL

Password/Pwd=password//Password when using SQL landing

For other key values, please refer to: https://www.cnblogs.com/niuyjdz/p/7533439.html

Four, the four parameters of the Open () function that connect to the database:

For multiple access to the database, open the database using _ConnectionPtr.Open (); for a visit, only _CommandPtr or _RecordSetPtr connections are needed.

_ConnectionPtr.Open(_bstr_t(connectionstr),_bstr_t(username),_bstr_t(pwd),option)//The last three parameters are not required and all have the default values

Parameter 1: the connection string, at least, must contain the server, the database parameter, when using the ConnectionString attribute of the object, the parameter can be “empty”, and it must not be converted into the _bstr_t type at this time, otherwise it must be converted into the type.

Parameter 2, parameter 3: when using sql sever to login, the required user name and password can be empty if provided in parameter 1.

Parameter 3: represents synchronous or asynchronous return. AdConnectUnspecified indicates that the connection is returned, that is, synchronization; adAsyncConnect indicates asynchronous return, and returns without connection. The default value is sync.

Five. Execute the SQL statement

1.ExecuteMethod (only _ConnectionPtr and _CommandPtr have this method).

_RecordSetPtr Execute(_bstr_t cmdtext,_variant_t* recordeffected,options)

Return value: it is very useful to be a recordset.

Parameter 1:sql statements must be converted into _bstr_t format

Parameter 2: returns a pointer whose value is the number of rows affected by the statement, which can be omitted.

Parameter 3: how to interpret parameter 1, usually AdCmdText, can be omitted, and the default value is also the value.

2.OpenMethod (method for executing _RecordSetPtr command)

_RecordSetPtr rs;

rs.CreateInstance(“adodb.recordset”);

rs.Open(cmdText,_variant_t((IDispatch*)pConnection)),adOpenKeyset,adLockOptimistic,adCmdText);

Parameter 1:sql command

Parameter 2: the pointer to the _ConnectionPtr that opens the database channel.

Parameter 2: the type of cursor is set or obtained by the CursorType property of the recordset.

Parameter 3: lock type used after opening recordset. Set or get by the LockType property. Reflect the way of updating after vernier editing (immediate update or delayed update)

Parameter 4: the way to parse the SQL command

Six. Close the database Close ():

After the three objects are used, they need to call the Close () function to close it and call Release () to release the resource.

You can also call Release (); the release () is to release the interface.

It can also be done without calling, because it is an intelligent pointer, which is automatically released when the smart pointer is destructed.

 

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *