首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

数据库中的数据更新不已 求帮助

2013-01-21 
数据库中的数据更新不了 求帮助for (int i 1 i 17 i++){SqlCommand command1 new SqlCommand(sel

数据库中的数据更新不了 求帮助
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);

[解决办法]
条件写的不对  string.Format("update dbo.localization set X = 'x',Y= 'y'  where SensorNumber = '{0}'", i)

热点排行