如何为sum按钮创建循环 [英] How to make a loop for sum button

查看:109
本文介绍了如何为sum按钮创建循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3列的表(用户名,从,拿)我想要的是制作循环到总和数

i在我的表单中有一个按钮它是UPDATE按钮

i have a table with 3 column ( username , from , take ) what i looking for is making loop to sum number
i have one button in my form it is UPDATE button

mycon.Open();
            string query = "UPDATE tbluser set take='" + txt1.text + "'";
            SqlDataAdapter sda = new SqlDataAdapter(query, mycon);
            sda.SelectCommand.ExecuteNonQuery();
            mycon.Close();



i想要制作另一个按钮,使得金额或计数从0开始



示例中的数字是记录数

我所拥有的是什么?b $ b


i want to make another button to make a sum or count number start with 0

the numbers in the example is the record numbers
that what i have

username . form . take
    test  . 0  . 2000
    test2 . 0  . 2000
    test3 . 0  . 2000



和我得到的结果我想找的是


and that what i result what i looking for

username . form . take
     test    .   0  . 2000
     test2   . 2000 . 2000
     test3   . 4000 . 2000





i想要测试总和(0 + 2000)但是得到的结果(test2(from))=(2000)



和test2(2000 + 2000)但是得到的结果(test3(from))=(4000)



我尝试了什么:



我是brogramming的新手,我搜索回合我没有找到



i want in test sum ( 0 + 2000 ) and but the result for sum in (test2(from)) =(2000)

and in test2 ( 2000 + 2000 ) and but the result for sum in (test3(from)) =(4000)

What I have tried:

i'm new in brogramming and i search bout that im didn't find

推荐答案

Quote:

现在怎么样



Ooooo。不,不 - 不是那样的。我知道你是初学者,但是,这非常危险!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:


Ooooo. No, no - not like that. I know you are a beginner, but, that's very dangerous! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期进行备份,不是吗?

在尝试继续之前修复整个应用程序...或者有人要为你删除你的数据库...



然后看问题 - 最简单的方法是使用SUM OVER:

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
Fix that throughout your whole app before you try to move on ... or somebody is going to delete your DB for you ...

Then look at the problem - the easiest way to do it is to use a SUM OVER:

SELECT Username, SUM(Take) OVER (ORDER BY Username) AS Form, Take FROM tbluser










Quote:

我需要一个参数化查询的例子来做它我不明白你试图给我什么

i need a example for Parameterized queries to do like it i didn't understand what you try to till me



你还没有被告知参数化查询? OMG。


You haven't been told about parameterized queries? OMG.

using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (myColumn1, myColumn2) VALUES (@C1, @C2)", con))
        {
        cmd.Parameters.AddWithValue("@C1", myValueForColumn1);
        cmd.Parameters.AddWithValue("@C2", myValueForColumn2);
        cmd.ExecuteNonQuery();
        }
    }


这篇关于如何为sum按钮创建循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆