C# npgsql字符串到数字
代码如下:C# npgsql字符串到数字,c#,sql,visual-studio-2010,npgsql,C#,Sql,Visual Studio 2010,Npgsql,代码如下: autoInsert.Parameters.Add(new NpgsqlParameter("price", NpgsqlDbType.Numeric)); autoInsert.Parameters[0].Value = txt_price.Text; con.Open(); autoInsert.ExecuteNonQuery(); con.Close(); 当我执行查询时,它显示错误:“输入字符串的格式不正确。” 如何将该字符串
autoInsert.Parameters.Add(new NpgsqlParameter("price", NpgsqlDbType.Numeric));
autoInsert.Parameters[0].Value = txt_price.Text;
con.Open();
autoInsert.ExecuteNonQuery();
con.Close();
当我执行查询时,它显示错误:“输入字符串的格式不正确。”
如何将该字符串转换为数字。
txt\U价格是文本框。在传递
数值时,您已经向Npgsql保证您传递了一个数字。
autoInsert.Parameters[0].Value = ConvertToInt32(txt_price.Text);
然后你传了一串
如果由于其他代码的原因,您已经确定txt\u price
中有一个十进制值,并且不可能有其他值,那么请使用:
autoInsert.Parameters[0].Value = decimal.Parse(txt_price.Text);
否则,在执行任何其他操作之前,请将其与代码结合以确保:
decimal price;
if(!decimal.TryParse(txt_price.Text, out price))
{
//code to display message that txt_price doesn't have a valid value.
return;
}
using(var con = /*your code that constructs the connection*/)
{
using(autoInsert = /*your code that returns to command*/)
{
autoInsert.Parameters.Add(new NpgsqlParameter("price", NpgsqlDbType.Numeric));
autoInsert.Parameters[0].Value = price;
con.Open();
autoInsert.ExecuteNonQuery();
con.Close();
}
}
txt_price.Text的内容是什么?伙计。。。这对你有用吗…?autoInsert.Parameters[“price”]。Value=price
使一点点内容更具可读性,也更易于维护