
string sql = "update Table1 set [Number]=@p1 where ID=1"
SqlConnection conn = new SqlConnection()
conn.ConnectionString = "Data Source=.\\SQLExpressIntegrated Security=trueAttachDbFilename=|DataDirectory|\\Database.mdfUser Instance=true"
SqlCommand cmd = new SqlCommand(sql, conn)
SqlParameter sp = new SqlParameter("@p1",SqlDbType.Decimal,38,18)
sp.Value=d
cmd.Parameters.Add(sp)
try
{
conn.Open()
return(cmd.ExecuteNonQuery())
}
catch (Exception)
{
return -1
throw
}
finally
{
conn.Close()
}
Number字段建议用Decimal(38,19)类型存储。
C#程序中查询数据时建议用Double或Decimal。
可以修改数据类型:alter table 表 alter column 字段 numeric(精度,2)
只用update是不行的。
其实为了数据的准确,你完全没有必要修改,有两个办法:
1.每次检索数据时用round函数取两位小数;
2.做一个视图,用round函数取两位小数。
一般来说,是显示双精度的。如果要保留多少位数,则需要用Round函数。。。例如 select round([字段1]/[字段2]+0.00000001,2)As [字段3] from 表1。
为什么非要在后面加上0.00000001呢?这是因为这个函数跟Excel里的稍有不同,你可以分别测试下这两个函数:round(0.5,0)和round(0.50000001,0),前者返回0,后者返回1。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)