2014-12-04 48 views
0

應用程序有沒有用戶輸入的,也不會是活在網絡上,所以SQL注入是不是威脅如何更新多個SQL行,其中表1的ID等於表2的ID

我的應用程序

此應用程序應該從1個表格向Google地理編碼器API發送多個地址,並將返回的緯度和經度存儲在第二個表格中供以後使用。API每天最多可以處理2500個請求。

問題是什麼

當我運行我的程序,沒有limmits,只有幾排(約20),獲得更新出來,都應該得到更新2500行。當我限制我的程序只更新10行以避免觸及API限制時,不會發生任何更改。我允許2500次請求時更新的大部分行都使用正確的值進行更新(除了少數幾個之外),現在我不擔心這一點,我認爲這只是地理編碼器的糟糕結果。

我有什麼到目前爲止

我有2串strLat和strLong持有從谷歌地理編碼API檢索lattitudes和經度。

string strLat = myCoordenates.Results[3].Geometry.Location.Lat.ToString(); 
string strLong = myCoordenates.Results[3].Geometry.Location.Lng.ToString(); 

我也有我的企圖用他們更新我的數據庫。這已經改變和操縱很多次了,我認爲這個問題很可能與我的這部分代碼有關。

using (SqlConnection myConnection = new SqlConnection(context)) 
    { 
    myConnection.Open(); 
    string strQueryUpdate = "UPDATE WEB_ARENA_GEO SET Lat = '" + strLat + "', Lng = '" + strLong + "'" + "WHERE ARENA_ID='" + row.ARENA_ID + "'"; 
    SqlCommand myCommandUpdate = new SqlCommand(strQueryUpdate, myConnection); 
    myCommandUpdate.ExecuteNonQuery(); 
    } 

以我數據的基礎上表1保持地址數據它具有PK ID以及一個稱爲ADDRESS_ID列,然後地址被分解等地址,城市,ST,ZIP ... ETC,表2持有緯度和經度,並且具有相同的等值的同一個ADDRESS_ID表,目前大部分的lats和lng都是0.00,它是十進制的。

表1

| --PKid-- | ADDRESS_ID |地址| CITY ..等
| 124246 | 70-00002913 | 112 bill st。 |鮑勃鎮

表2

| ADDRESS_ID | Lat | Lng |
| 70-00002913 | 0.00 | 0.00 |

整個更新控制器

//GET: /TABLE2/Update/ 

    public ActionResult Update(string id) 
    { 
     IEnumerable<TABLE1> table1 = db.TABLE1; 
     IEnumerable<TABLE2> table2 = db.TABLE2; 
     string context = "my database context here"; 

     foreach (var row in table1.AsEnumerable().Take(10)) 
     { 

      string strAddr = row.ADDRESS + "," + row.CITY + "," + row.ZIP + "," + row.COUNTRY; 

      GoogleMapsDll.GeoResponse myCoordenates = new GoogleMapsDll.GeoResponse(); 
      myCoordenates = GoogleMapsDll.GoogleMaps.GetGeoCodedResults(strAddr); 
      if (myCoordenates.Results != null && myCoordenates.Results.Length > 3) 
      { 
       string strLat = myCoordenates.Results[3].Geometry.Location.Lat.ToString(); 
       string strLong = myCoordenates.Results[3].Geometry.Location.Lng.ToString(); 
       using (SqlConnection myConnection = new SqlConnection(context)) 
        { 
         myConnection.Open(); 
         string strQueryUpdate = "UPDATE WEB_ARENA_GEO SET Lat = '" + strLat + "', Lng = '" + strLong + "'" + "WHERE ADDRESS_ID='" + row.ADDRESS_ID + "'"; 
         SqlCommand myCommandUpdate = new SqlCommand(strQueryUpdate, myConnection); 
         myCommandUpdate.ExecuteNonQuery(); 
        } 
       db.SaveChanges(); 
      } 

     } 
     return RedirectToAction("Index"); 

如果任何其他信息將是有益的,請簡單地問了意見,我會盡我所能提供

+1

我不是其所以然清楚,你不知道該怎麼在這裏做?它在UPDATE中使用JOIN嗎? – 2014-12-04 14:32:56

+4

你好SQL注入 – 2014-12-04 14:33:28

+0

我需要能夠更新表2中的行與正確的緯度和經度與表1中相同的ID相匹配 – RyeNyeTheComputerScienceGuy 2014-12-04 14:34:18

回答

1

它看起來像你的地方條件定義不正確。您正在發送,其中以下:

"WHERE'" + row.ADDRESS_ID + " = '" + row1.ADDRESS_ID + "' 

這基本上是對海誓山盟比較實際ID值。我相信,你需要修改你的查詢如下:

string strQueryUpdate = "UPDATE TABLE2 SET Lat = " + strLat + ", Lng = " + strLong + 
     " WHERE ADDRESS_ID='" + row.ADDRESS_ID + "'"; 

UPDATE

var pageSize = 10; 
var totalPages = Math.Ceiling(table1.Count()/pageSize); 

for(var i = 0; i < totalPages; i++) { 
     var addressesToProcess = table1.Skip(i * pageSize).Take(pageSize); 
     foreach (var address in addressesToProcess) 
     { 
      string strAddr = address.ADDRESS + "," + address.CITY + "," + address.ZIP + "," + address.COUNTRY; 

      GoogleMapsDll.GeoResponse myCoordenates = new GoogleMapsDll.GeoResponse(); 
      myCoordenates = GoogleMapsDll.GoogleMaps.GetGeoCodedResults(strAddr); 
      if (myCoordenates.Results != null && myCoordenates.Results.Length > 3) 
      { 
       string strLat = myCoordenates.Results[3].Geometry.Location.Lat.ToString(); 
       string strLong = myCoordenates.Results[3].Geometry.Location.Lng.ToString(); 
       using (SqlConnection myConnection = new SqlConnection(context)) 
       { 
        myConnection.Open(); 
        string strQueryUpdate = "UPDATE WEB_ARENA_GEO SET Lat = '" + strLat + "', Lng = '" + strLong + "'" + "WHERE ADDRESS_ID='" + address.ADDRESS_ID + "'"; 
        SqlCommand myCommandUpdate = new SqlCommand(strQueryUpdate, myConnection); 
        myCommandUpdate.ExecuteNonQuery(); 
       } 
       db.SaveChanges(); 
      } 
     } 
} 
+0

我應該知道這一個,但不幸的是它仍然沒有解決這個問題,我的行仍然沒有更新與拉特和lngs。 – RyeNyeTheComputerScienceGuy 2014-12-05 14:30:00

+1

您是否可以確認表1中的每個address_id在表2中都有相同的address_id對應的記錄? – mreyeros 2014-12-05 14:32:03

+0

是的,我可以證實這一點。 100%確定。 ID的起始位置爲70-00002913,然後依次遞增1,直到70-00005528 – RyeNyeTheComputerScienceGuy 2014-12-05 14:35:19

相關問題