用if else C#选择sum() [英] Select sum() with if else C#

查看:89
本文介绍了用if else C#选择sum()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在MySql数据库表中选择列值的总和,如果结果大于0,则在文本框中显示结果如果列中没有任何内容,则在文本框中打印0,但代码会显示只要列中没有任何内容,输入字符串的格式就不正确,而不是0。请帮忙!



我尝试过:



 尝试 
{
MySqlConnection connection = new MySqlConnection( datasource = localhost; username = root; password =);
connection.Open();
string str = 选择总和(amount_received_today )来自cmmf.budgeted_income其中received_from =' +
.txtRfrom_Income.Text + ';
MySqlCommand com = new MySqlCommand(str,connection);
int count = Convert.ToInt32(com.ExecuteScalar()。ToString());
connection.Close();
if (count == 0
{
txtRamount_Income。 Text = 0
}
else
{
txtRamount_Income.Text = +计数;
}
}
catch (例外情况)
{
MessageBox.Show(ex.Message) ;
}

解决方案

Quote:

但代码带来的声明是输入字符串格式不正确而不是0



除了确切的错误消息之外,错误的位置也有助于理解错误。

try / catch集团肯定是一件好事,但它也阻止程序告诉你问题出在哪里。

-----

  int  count = Convert.ToInt32(com.ExecuteScalar()。ToString()); 



ExecuteScalar()返回一个转换为字符串的数值,并重新转换为整数。如果ExecuteScalar()的值不是整数,则会出现错误。

尝试

  int  count = com.ExecuteScalar(); 



建议:使用调试器来编写代码,看看出了什么问题。

-----

  string  str =  从cmmf.budgeted_income中选择sum(amount_received_today)其中received_from =' + this.txtRfrom_Income.Text +  '; 



不是你问题的解决方案,而是另一个问题你有。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


I want to select sum of column values in MySql database table and show the result in a textbox if the result is greater than 0 else print 0 in the textbox if there is nothing in the column but the code brings a statement that 'Input string was not in correct format' instead of 0 whenever there is nothing in the column. Any help please!

What I have tried:

try
{
   MySqlConnection connection = new MySqlConnection("datasource=localhost;username=root;password=");
   connection.Open();
   string str = "select sum(amount_received_today) from cmmf.budgeted_income where received_from ='" +
        this.txtRfrom_Income.Text + "'";
   MySqlCommand com = new MySqlCommand(str, connection);
   int count = Convert.ToInt32(com.ExecuteScalar().ToString());
   connection.Close();
   if (count == 0)
   {
      txtRamount_Income.Text = "0"
   }
   else
   {
      txtRamount_Income.Text = "" + count;
   }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

解决方案

Quote:

but the code brings a statement that 'Input string was not in correct format' instead of 0


Along with exact error message, the position of error also help to understand what is wrong.
The try/catch bloc is certainly a nice thing, but it also prevent the program from telling you where is the problem.
-----

int count = Convert.ToInt32(com.ExecuteScalar().ToString());


ExecuteScalar() returns a numeric value that you convert to string and that you reconvert to integer. If the value of ExecuteScalar() was not an integer, you get an error.
Try

int count = com.ExecuteScalar();


Advice: use the debugger to whatch you code performing and see where things go wrong.
-----

string str = "select sum(amount_received_today) from cmmf.budgeted_income where received_from ='" +this.txtRfrom_Income.Text + "'";


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


这篇关于用if else C#选择sum()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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