Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将返回到textbox的SQL查询中的数字四舍五入到第二位小数_C#_Sql_Sql Server_Winforms_Visual Studio - Fatal编程技术网

C# 如何将返回到textbox的SQL查询中的数字四舍五入到第二位小数

C# 如何将返回到textbox的SQL查询中的数字四舍五入到第二位小数,c#,sql,sql-server,winforms,visual-studio,C#,Sql,Sql Server,Winforms,Visual Studio,我有3个文本框,从已经使用ROUND函数的3个SQL查询中接收int值。但是文本框中的结果返回4个小数点。。ie我需要值xxxxx.xx,但SQL查询返回xxxx.xxxx 这是我的密码 private void totalRef_btn_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection("Data source=10.0.0.3,1434;Initial Catalog=client_o

我有3个文本框,从已经使用ROUND函数的3个SQL查询中接收int值。但是文本框中的结果返回4个小数点。。ie我需要值xxxxx.xx,但SQL查询返回xxxx.xxxx

这是我的密码

private void totalRef_btn_Click(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection("Data source=10.0.0.3,1434;Initial Catalog=client_orders_test;**Logins omitted**");

    SumTotalquery = "Select ROUND(SUM(value),2) from costings_cur";
    SumExVatquery = "Select ROUND(SUM(value-(value*14/100)),2) from costings_cur";
    SumVatquery = "Select ROUND(SUM(value*14/100),2) from costings_cur";

    SqlCommand totalQry = new SqlCommand(SumTotalquery, conn);
    SqlCommand exVatQry = new SqlCommand(SumExVatquery, conn);
    SqlCommand vatQry = new SqlCommand(SumVatquery, conn);

    conn.Open();
    SqlDataReader DR1 = totalQry.ExecuteReader();

    if (DR1.Read())
    {
        total_txtbox.Text = DR1.GetValue(0).ToString();
    }

    conn.Close();

    conn.Open();

    SqlDataReader DR2 = exVatQry.ExecuteReader();

    if (DR2.Read())
    {
        exvat_txtbox.Text = DR2.GetValue(0).ToString();
    }

    conn.Close();

    conn.Open();

    SqlDataReader DR3 = vatQry.ExecuteReader();

    if (DR3.Read())
    {
        vat_txtbox.Text = DR3.GetValue(0).ToString();
    }

    conn.Close();
}
我甚至试过用这个:

exvat_txtbox.Text = DR2.GetValue(0).ToString().Trim();
提前感谢

使用小数18,2获得正确结果

SELECT CAST(ROUND(SUM(value),2) AS DECIMAL(18,2)) FROM costings_cur
类似的方式为其他人选择stations

注意:函数只对指定长度的数字进行四舍五入,不负责获取特定格式的数据

使用数字格式字符串,例如ToString0

使用搜索避免重复的问题


DR1.GetValue0.ToString。;?可能重复-但为什么要启动3个连接-您可以在一个连接/查询中完成这一切,只返回3列。您应该能够使用类似于DR1.GetValue0.ToString的格式说明符。有关详细说明和其他选项,请参阅@BryanWoodford谢谢,但在这种情况下占位符不起作用。尝试使用ToString0.00;如果运气不好,..@JiggsJedi,这个问题与sql中的sql查询有关,而不是在远程查询数据库时。如果我在SSMS中使用我的初始查询,我会得到期望的结果。但不是我的申请。问题是C将结果返回到文本框。感谢您的帮助。您的答案非常有效: