顺便Numeric
说一句,你已经向 Npgsql 保证你传递了一个数字。然后你传递了一个字符串。
如果您已经确定,由于其他代码,其中有一个十进制值txt_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();
}
}