如何将数据从数据库显示到两个小数位的文本框中 [英] How to display numeric data from database to textbox in two decimal places

查看:88
本文介绍了如何将数据从数据库显示到两个小数位的文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好:)



我想知道在从数据库加载数据后如何在我的文本框中只显示2位小数。谁能帮我?这是关于检索数据并将其放入文本框中的代码:)







Hello :)

I would like to know on how to display to my textbox only 2 decimal places after loading the data from the database. Can anyone help me? here is my code on retrieving the data and put it inside the textbox :)



SqlCommand cmd = new SqlCommand("SELECT * from tblMiscellaneousFee WHERE GradeLevel = '" + grd_lvl.Text + "'", MySqlConnection);
               SqlDataReader rred = cmd.ExecuteReader();
               while (rred.Read())
               {
                   bookfee.Text = rred[2].ToString();
                   PEFee.Text = rred[3].ToString();
                   SchuniformFee.Text = rred[4].ToString();
                   MedicFee.Text = rred[5].ToString();
                   MiscFee.Text = rred[6].ToString();
                   tuitionFee.Text = rred[7].ToString();
                   totMisc.Text = MiscFee.Text;


               }
               rred.Close();

推荐答案

假设你的图书费用是数值(如果不是,那么使用右边的重视自己)。



首先,将其转换为适当的数字类型(在匹配数据库列数据类型的情况下适用 - 所以如果它是FLOAT,请使用 float double ;如果是DECIMAL,请使用 decimal 。如果是NVARCHAR,请更改数据库事实并非如此 - 您应该始终将值存储在最合适的数据类型中,否则以后会出现大问题。



然后,将数据格式化为字符串。

Assuming that it's your book fee that is the numeric value (if it isn't, then use the right values yourself).

First, convert it to an appropriate numeric type (appropriate in the case matching your DB column data type - so if it's FLOAT, use float or double; if it's DECIMAL use decimal. If it's NVARCHAR, change your database so it isn't - you should always store values in the most appropriate data type or it will give huge problems later on.

Then, format the data to a string.
bookfee.Text = string.Format("{0:0.00}", (double) rred[2]);





BTW:你正在做的事情结合起来制造一个令人讨厌的错误来源:你不是要指定要检索的列(而是获得所有列),并使用数字偏移来指定你使用的列。

如果你必须使用 SELECT * FROM ... - 并且它非常无效,特别是如果你没有使用所有值,因为你不是 - 那么你真的必须使用列名而不是数字偏移。否则,你依赖于可以在程序之外更改的列顺序 - 此时事情开始变得非常巧妙和严重错误:如果你不是非常非常小心,你可以真正搞砸你的数据库。列出你想要的列,只列出那些列,并使用列名从你的阅读器中访问它们。

最重要的是:不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。你在那里的代码允许任何用户只需输入一个文本框就可以删除你的数据库......



BTW: You are doing things which combine to make one nasty source of bugs: you aren;t specifying the columns you want to retrieve (instead you get all of them), and you use numeric offsets to specify which column you use.
If you must use SELECT * FROM ... - and it's very inefficent, particularly if you aren't using all values, as you aren't - then you really have to use column names rather than numeric offsets. Otherwise you are relying on a column order that can be changed outside of your program - at which point things start to go very subtly and badly wrong: you can really muck up your database if you aren't very, very careful. List the columns you want, and only those, and use column names to access them from your reader.
Ans most seriously of all: Do not 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. Use Parametrized queries instead. The code you have there allows any user to delete your database just by typing in a text box...


这篇关于如何将数据从数据库显示到两个小数位的文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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