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后应如何写??
请教 更新数据库中X和Y字段更新为计算出来的x和y值,X和Y字段是float型 计算出的x和y是double类型
如上述写报错 char不能转换为float set后应如何写??
{
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型的
手动拼字符串还要做参数安全性检查,比较麻烦。