如何从文本框中获取十进制值并将其保存在SQL Server中
我有win格式的文本框
用户提供输入
i我不确定哪个合成用户类型值(10或10.0或10.00)
i将此值存储到sql server数据库
并希望此值为(10.00格式为10或10.0或10.00)
sql列数据类型为浮动
当我在sql中看到表时,我看到值为10或10.0或10.00用户类型
但我希望当用户输入10或10.0时这个值为10.00
我该如何处理这种情况
有人可以一步一步解释吗?
我的尝试:
使用(SqlConnection conn = new SqlConnection(con))
{
using(SqlCommand cmd = new SqlCommand(usp_weightinsertdetails,conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@ Date,dateTimePicker1 。值) ;
cmd.Parameters.AddWithValue(@ ProductName,textBox1.Text);
cmd.Parameters.AddWithValue(ProductPieces,int.Parse(textBox2.Text) ));
浮动重量,个人电脑,avr;
重量= float.Parse(textBox3.Text.tostring(0.00)) );
pc = float.Parse(textBox2.Text);
avr =重量/ pc;
cmd.Parameters.AddWithValue(@ ProductWeight,weight.tostring(0.00));
cmd.Parameters.AddWithValue(@ AveragePieceWeight,avr.tostring(0.00)) ;
conn.Open();
cmd.ExecuteNonQuery();
i have text box in win form
in which user give input
i am not sure in which formate user type value (either 10 or 10.0 or 10.00)
i am storing this value to sql server database
and want this value in (10.00 format either 10 or 10.0 or 10.00)
sql column data type is float
when i saw in table in sql i saw value as 10 or 10.0 or 10.00 which user type
but i want this value as 10.00 when user type 10 or 10.0
how can i manage this situation
can someone explain step by step ?
What I have tried:
using (SqlConnection conn = new SqlConnection(con))
{
using (SqlCommand cmd = new SqlCommand("usp_weightinsertdetails", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Value);
cmd.Parameters.AddWithValue("@ProductName", textBox1.Text);
cmd.Parameters.AddWithValue("ProductPieces", int.Parse(textBox2.Text));
float weight, pc, avr;
weight = float.Parse(textBox3.Text.tostring("0.00"));
pc = float.Parse(textBox2.Text);
avr = weight / pc;
cmd.Parameters.AddWithValue("@ProductWeight", weight.tostring("0.00"));
cmd.Parameters.AddWithValue("@AveragePieceWeight", avr.tostring("0.00"));
conn.Open();
cmd.ExecuteNonQuery();
首先,将用户输入转换为数字:
First, convert the user input to a number:
double userInput;
if (!double.TryParse(myTextBox.Text, out userInput))
{
// Report a problem with what he typed.
...
return;
}
然后将值作为参数传递,就像你一样,但是使用userInput而不是文本框内容。
[edit]一行在外面代码块。[/ edit]
Then pass the value as a parameter, just like you are, but using userInput instead of the text box content.
[edit]One line was outside the code block.[/edit]