One-step compute Mining

Source: Internet
Author: User

A few days ago, when I checked out the data table, the Single Column summation of the datagridview control had a hard time.
Now I almost got into the habit of thinking about some things and won't come to Baidu first. Here I first thought about the checkout sum for the first version of the data center:
The code for intercepting the Recharge Amount snippet is as follows:

'Read the Recharge Amount strsql = "select sum (addmoney) as addcash from recharge_info where userid = '" & trim (combooperatorusername. text) & "'and date ='" & format (date, "yyyy-mm-dd ") & "'and status ='" & "Unsettled" & "'having sum (addmoney) is not null" set mrcrecharge = executesql (strsql, msgtext) If mrcrecharge. EOF = true then txtchargecash. TEXT = 0 else txtchargecash. TEXT = mrcrecharge ("addcash "). value 'Return card amount end if

In the beginning, I simply wrote like a little white as below:

 

Strsql = "select sum (t_charge.addmoney) from t_charge where [email protected] and [email protected] and status = 'false'" dim parameters as sqlparameter () = {New sqlparameter ("@ userid", chargeinfo. userid), new sqlparameter ("@ date", chargeinfo. returncarddate)} 'Add the dt = HELP parameter to the SQL statement. executeselectstrsql, commandtype. text, parameters)


 

In fact, at first, I thought this was definitely not feasible, but what is wrong? It must be a sum SQL statement!
After some tests, the system prompts "The addmoney Column cannot be found"
As a result, I began to feel confused and decided to ask du Niang ......
Since they are not biological, du Niang did not give the desired answer, almost all of them were the same as above. Finally, you have to rely on yourself.
After breakpoint debugging, the U layer first shows the problem.
Txtchargemoney. Text = dtallchargemoney. Rows (0) ("addmoney"). tostring ()
Txtchargemoney. Text here cannot receive the value.
This time I got a little more calm, clarified my thoughts, changed the keyword, including "VB.net, datagridview, and a column sum", and finally found the compute keyword! It is also the key point of this problem.

Datatable. compute method:
Calculates the given expression on the current row that is used to pass filtering conditions.
Syntax: Public Function compute (expression as string, filter as string) as object
Parameters:
Expression type: system. String, the expression to be calculated. It can be understood as "the aggregate function required by the parameter ".
Filter Type: system. String. to restrict the filter of rows computed in an expression, you can understand that filter determines which rows are used in the expression.
Return Value Type: system. Object
Object, set as calculation result. If the expression is null, the return value is dbnull. value.

Well, based on the above information, the SQL statement should be written:

(Layer d) strsql = "select t_charge.addmoney from t_charge where [email protected] and [email protected] and status = 'false '"
(U layer) txtchargemoney. Text = dtallchargemoney. Compute ("sum (addmoney)", "true"). tostring ()

 

Then, the Single Column summation problem is solved. What should I do if the two columns are computed before the summation? For example, the sum of column A and column B.
We assume that: Compute ("sum (A * B), but this is actually wrong!
Positive Solution: in this case, assume that there is a datacolumn named "c" and the expression attribute is set to "a * B"
The expression parameter of the compute method is sum (c ).

Conclusion: When you encounter problems, you still need to be patient and find out the key points. programming is not a "Anxious job ". Discovering, solving, and expanding your knowledge are one of the most effective ways to learn quickly.

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.