Ado judges SQL statements that affect rows

Source: Internet
Author: User

When using ADO to operate a database, you often need to determine the number of rows affected by the current SQL statement. The execute method (ADO command) can implement this function.

 

RunCommandtextThe query, SQL statement, or stored procedure specified in the property.

Syntax

For rowsCommand:

Set Recordset=Command.Execute (Recordsaffected,Parameters,Options)

For non-return by rowCommand:

Command.Execute Recordsaffected,Parameters,Options

Return Value

ReturnRecordsetObject reference.

Parameters

RecordsaffectedOptional,Long IntegerVariable, the number of records affected by the operation returned by the provider.RecordsaffectedParameters are only applicable to Operation queries or stored procedures.RecordsaffectedThe number of records returned by query results or stored procedures is not returned. For more information, seeRecordcountAttribute.

ParametersOptional,ShapingArray, which is a parameter value transmitted using SQL statements. (If this parameter is used for transmission, the output parameter does not return the correct value .)

OptionsOptional,Long IntegerValue, indicating how the provider calculatesCommandObjectCommandtextAttribute. This value can be one of the following constants:

Constant Description
Adshorttext Indicates that the provider should be calculated according to the command text definition (such as SQL statement)Commandtext.
Adcmdtable Instruct ADO to generate SQL queriesCommandtextAll rows are returned in the named table.
Adcmdtabledirect Indicates that the provider shouldCommandtextAll rows are returned in the named table.
Adcmdstoredproc Indicates that the provider should be calculated based on the Stored ProcedureCommandtext.
Adcmdunknown InstructionsCommandtextThe command type in is unknown.
Adasyncexecute Indicates that the command should be executed asynchronously.
Adasyncfetch IndicatesCachesizeThe remaining rows after the initial quantity specified by the attribute should be extracted asynchronously.

For more information about the first four constants in this list, seeCommandtypeAttribute.

Description

UseCommandObjectExecuteThe method can be executed in the object'sCommandtextAttribute. IfCommandtextAttribute specifies that the query is returned by row, and any results generated by the execution will be stored in the newRecordsetObject. If the query is not returned by line, the provider returns the disabledRecordsetObject. Some application languages allow this return value to be ignored (if you do not need anyRecordset).

If the query contains parametersCommandThe current value of the parameter in the object, unlessExecuteThe parameters transmitted by the call overwrite them. You can callExecuteTo overwrite the subset of parameters by omitting the new values of some parameters. The order of the specified parameter is the same as that transmitted in the method. For example, if you have four (or more) parameters and want to transmit new values only for the first and fourth parameters, you can use array (var1, var4)ParametersParameter transfer.

Note:InParameters When the parameter is transferred, the output parameter does not return the correct value.

After this operation is completedExecutecompleteEvent.

 

Demo:

_ Variant_t vrecordsaffected;

Try
{
M_pconnection-> execute (_ bstr_t (ssql), & vrecordsaffected, ad1_text );
}

Vrecordsaffected --- indicates the number of affected rows of the current SQL statement.

 

However, a problem occurs in the project. If a table has a trigger, and the trigger does not succeed every time (such as update ),

When an insert statement is executed, the update operation fails. In this case, the vrecordsaffected Value Returns zero.

When vrecordsaffected is used as the return value to determine whether the insert operation is successful, the data cannot be normally stored into the database. The analysis and query analyzer can obtain the status of the affected row twice. But how to implement this function in VC, this problem has not been solved .....

 

Later, I switched to the previously written pipeline.
 

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.