数据库中的数据更新不了 求帮助
for (int i = 1; i < 17; i++)
{
SqlCommand command1 = new SqlCommand("select * from dbo.localization where SensorNumber = " & i & " ", conn);
SqlDataReader drl = command1.ExecuteReader();
//while (drl.Read())
drl.Read();
double d1, d2, d3, d4;
double x, y;
x = 0;
y = 0;
int d = 40;
int D = 30;
//sensorN = int.Parse(drl["SensorNumber"].ToString());
//int N = Convert.ToInt16(drl["SensorNumber"]);
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;
//x=1;y=2;
//ExeCmd("update dbo.localization set X ='x',Y='y' where SensorNumber = 'N'");
}
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();
//ExeCmd("update dbo.localization set X ='x',Y='y' where SensorNumber = 'N'");
SqlCommand cmd = new SqlCommand("update dbo.localization set X = ‘x’,Y= ‘y’ where SensorNumber = 'i '", conn);
cmd.ExecuteNonQuery();
}
更新不了 怎么回事?求帮助 还有第一句选择语句where SensorNumber = " & i & " i是个变量 好像这样表达不对 应该怎么样表达?
[解决办法]
没有更新应该是where条件不成立
修改如下,试试
SqlCommand cmd = new SqlCommand(string.Format("update dbo.localization set X = 'x',Y= 'y' where SensorNumber = '{0}'", i), conn);