c#sql执行时,在向服务器发送请求时发生传输级错误。和,参数值“0.000000000000000000”超出范围。错误...

it2022-05-09  30

 

sql部分

create table tbmodel1( id bigint primary key identity(1,1), about_id bigint , price decimal(18,2) , )

  

c#代码

public long Add(model1Model model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into tbmodel1(about_id,price) values (@about_id,@price);select @@IDENTITY"); SqlParameter[] parameters = { new SqlParameter("@about_id", SqlDbType.BigInt,8), new SqlParameter("@price", SqlDbType.Decimal), }; //foreach (var a in new int[] { 1 }) { parameters[a].Scale = 18; parameters[a].Precision = 2; }//原来错的 foreach (var a in new int[] { 1 }) { parameters[a].Precision = 18; parameters[a].Scale = 2; }//对的, parameters[0].Value = model.about_id; parameters[1].Value = model.price;//参数值“0.000000000000000000”超出范围。???再看下, object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);//执行sql返回最后更新的id, //报错:有一次是,在向服务器发送请求时发生传输级错误。,,,网上说改web.config的 Pooling=false,没用, //报错:参数值“0.000000000000000000”超出范围。(这两个错误,有时是这个,有时是上面一个,有点奇怪,不知哪里控制) if (obj == null) { return 0; } else { return Convert.ToInt64(obj); } }

  

 

说明:它在,DbHelperSQL.GetSingle()里,执行到, ExecuteScalar() 时,报错,查了半天(因为原来字段很多,现在简化了),结果是Precision,和Scale的设置,给弄反了,

正确://new int[] { 1 },是 @price 的下标是1,如果有多个,这里就可以,new int[] { 1,3,6 ... } 之类的, foreach (var a in new int[] { 1 }) { parameters[a].Precision = 18; parameters[a].Scale = 2; }//对的,

 

转载于:https://www.cnblogs.com/ijunxiong/articles/8278206.html


最新回复(0)