更新数据库问题
SqlCommand cmd = new SqlCommand(string.Format("update dbo.localization set X = ' "+x+"',Y='"+y+"' where SensorNumber = '{0}'", i), conn);请教 更新数据库中X和Y字段更新为计算出来的x和y值,X和Y字段是float型 计算出的x和y是double类型
如上述写报错 char不能转换为float set后应如何写?? --------------------编程问答-------------------- 你把数据库中的类型改为double吧 --------------------编程问答-------------------- 写个强转的方 吧 --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 断点看看,最终执行的sql语句是什么,仅看这么一句,貌似没什么错误. --------------------编程问答-------------------- set X = ' "+x+"',Y='"+y+"' 就是把char类型的x和y赋给float类型的X和Y了 应该怎么改? --------------------编程问答-------------------- set X = cast('"+x+"' as float) --------------------编程问答-------------------- 从数据类型 varchar 转换为 float 时出错。 还是不行啊 --------------------编程问答-------------------- 去掉单引号。。。 --------------------编程问答-------------------- 单引号去掉。。 --------------------编程问答-------------------- 为什么去掉点引号后报错 列名非数字无效。。。。 --------------------编程问答-------------------- X和Y是什么类型是,代码都贴出来
--------------------编程问答-------------------- for (int i = 1; i < 17; i++)
{
SqlCommand command1 = new SqlCommand("select * from dbo.localization where SensorNumber ='"+i+"'", conn);
SqlDataReader drl = command1.ExecuteReader();
drl.Read();
double d1, d2, d3, d4;
double x, y;
x = 0;
y = 0;
int d = 40;
int D = 30;
if ((!drl["RSSI1"].Equals(DBNull.Value)) &&(!drl["RSSI2"].Equals(DBNull.Value)))
{
d1 = Convert.ToDouble(drl["RSSI1"]);
d2 = Convert.ToDouble(drl["RSSI2"]);
double cosB = (d1*d1+d*d-d2*d2)/2*d1*d;
double sinB = Math.Sqrt(1-cosB*cosB);
x = cosB * d1;
y = sinB * d1;
}
else if ( (!drl["RSSI1"].Equals(DBNull.Value)) && (!drl["RSSI4"].Equals(DBNull.Value)))
{
d1 = Convert.ToDouble(drl["RSSI1"]);
d4 = Convert.ToDouble(drl["RSSI4"]);
double cosB = (d1*d1+D*D-d4*d4)/2*d1*D;
double sinB = Math.Sqrt(1-cosB*cosB);
x = sinB*d1;
y = cosB*d1;
}
else if ((!drl["RSSI3"].Equals(DBNull.Value)) && (!drl["RSSI4"].Equals(DBNull.Value)))
{
d3 = Convert.ToDouble(drl["RSSI3"]);
d4 = Convert.ToDouble(drl["RSSI4"]);
double cosB = (d4*d4+d*d-d3*d3)/2*d4*d;
double sinB = Math.Sqrt(1-cosB*cosB);
x = cosB*d4;
y = D - sinB*d4;
}
else if ((!drl["RSSI2"].Equals(DBNull.Value)) && (!drl["RSSI3"].Equals(DBNull.Value)))
{
d2 = Convert.ToDouble(drl["RSSI2"]);
d3 = Convert.ToDouble(drl["RSSI3"]);
double cosB = (d2*d2+D*D-d3*d3)/2*d2*D;
double sinB = Math.Sqrt(1-cosB*cosB);
x = d-sinB*d2;
y = cosB*d2;
}
else
{
x = 0;
y = 0;
}
drl.Close();
SqlCommand cmd = new SqlCommand(string.Format("update dbo.localization set X = "+x+",Y= "+y+" where SensorNumber = '{0}'", i), conn);
cmd.ExecuteNonQuery();
}
代码写的比较乱 见笑 X和Y 数据库中定义的是float型的 --------------------编程问答-------------------- 单引号的问题,不该加的时候别加~~~~ --------------------编程问答-------------------- 如果实在不知道怎么格式化数据格式,可以由SqlParameter自动进行格式化处理。
手动拼字符串还要做参数安全性检查,比较麻烦。
补充:.NET技术 , ASP.NET