C# 如何更新表1';s ID等于表2';身份证

C# 如何更新表1';s ID等于表2';身份证,c#,sql-server,asp.net-mvc-5,C#,Sql Server,Asp.net Mvc 5,应用程序没有用户输入,也不会在web上运行,所以sql注入不是一种威胁 我的应用程序 这个应用程序应该将一个表中的多个地址发送到Google的geocoder API,并将返回的纬度和经度存储在第二个表中供以后使用。该API每天最多有2500个请求 问题出在哪里 当我在没有限制的情况下运行程序时,应该更新的2500行中只有少数行(大约20行)得到了更新。当我将程序限制为仅更新10行以避免达到API限制时,不会发生任何更改。当我允许2500个请求时,大多数确实更新的行都用正确的值进行了更新,只有少

应用程序没有用户输入,也不会在web上运行,所以sql注入不是一种威胁

我的应用程序

这个应用程序应该将一个表中的多个地址发送到Google的geocoder API,并将返回的纬度和经度存储在第二个表中供以后使用。该API每天最多有2500个请求

问题出在哪里

当我在没有限制的情况下运行程序时,应该更新的2500行中只有少数行(大约20行)得到了更新。当我将程序限制为仅更新10行以避免达到API限制时,不会发生任何更改。当我允许2500个请求时,大多数确实更新的行都用正确的值进行了更新,只有少数例外,我现在不担心这一点,我认为这只是地理编码器的坏结果

到目前为止我拥有的

    //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");
我有两个字符串strLat和strLong,它们保存从google geocoder API检索到的晶格和经度

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的列,然后地址被分解为address、CITY、ST、ZIP…等,表2包含纬度和经度,也有相同的address_ID表,具有相同的相等值,目前大多数lats和LNG为0.00,类型为decimal

表1

|--PKid——|地址| ID |地址|城市..等
|124246 | 70-00002913 | 112比尔街|鲍勃镇

表2

|地址| 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");
//获取:/TABLE2/更新/
公共操作结果更新(字符串id)
{
IEnumerable table1=db.table1;
IEnumerable table2=db.table2;
string context=“此处为我的数据库上下文”;
foreach(表1.AsEnumerable()中的var行。Take(10))
{
字符串strAddr=row.ADDRESS+“,“+row.CITY+”,“+row.ZIP+”,“+row.COUNTRY;
googlemapsdl.georresponse mycordenates=新的googlemapsdl.georresponse();
mycordenates=googlemapsdl.GoogleMaps.GetGeoCodedResults(strAddr);
if(mycordenates.Results!=null&&mycordenates.Results.Length>3)
{
字符串strLat=mycordenates.Results[3].Geometry.Location.Lat.ToString();
字符串strLong=mycordenates.Results[3].Geometry.Location.Lng.ToString();
使用(SqlConnection myConnection=newsqlconnection(context))
{
myConnection.Open();
string strQueryUpdate=“UPDATE WEB_ARENA_GEO SET Lat=”“+strLat+”,Lng=”“+strLong+”“+”,其中ADDRESS_ID=”“+row.ADDRESS_ID+”;
SqlCommand myCommandUpdate=新的SqlCommand(strQueryUpdate,myConnection);
myCommandUpdate.ExecuteOnQuery();
}
db.SaveChanges();
}
}
返回操作(“索引”);

如果有任何其他信息有帮助,请在评论中询问,我将尽力提供您的where条件定义不正确。您当前正在向where发送以下信息:

 "WHERE'" + row.ADDRESS_ID + " = '" + row1.ADDRESS_ID + "'
这基本上是将实际id值相互比较。我认为您需要将查询修改为以下内容:

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

 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();
           }
      }
 }
var pageSize=10;
var totalPages=Math.天花(表1.Count()/pageSize);
对于(变量i=0;i3)
{
字符串strLat=mycordenates.Results[3].Geometry.Location.Lat.ToString();
字符串strLong=mycordenates.Results[3].Geometry.Location.Lng.ToString();
使用(SqlConnection myConnection=newsqlconnection(context))
{
myConnection.Open();
string strQueryUpdate=“UPDATE WEB_ARENA_GEO SET Lat=”“+strLat+”,Lng=”“+strLong+”“+”,其中ADDRESS_ID=”“+ADDRESS.ADDRESS_ID+”;
SqlCommand myCommandUpdate=新的SqlCommand(strQueryUpdate,myConnection);
myCommandUpdate.ExecuteOnQuery();
}
db.SaveChanges();
}
}
}

我不太清楚您不知道在这里怎么做?是在更新中使用联接吗?我需要能够使用正确的lat和lng更新表2中的行,这些行与表1中的相同ID匹配是的,您需要参数化您的更新脚本。我们将对此进行研究,但这也会解决我的问题吗?我的行不全是我的s正在更新?但是在我的整个应用程序中只有几个选定的行?在与响应相同的代码中有一个SqlConnection。重定向…只是伤了我的眼睛。我应该知道这一点,但不幸的是它仍然没有解决问题,我的行仍然没有使用lat和lngs更新。您能确认表1中的每个地址\u id都有表2中具有相同地址_id?的对应记录,因此从外观上看,地址_id是一个字符串列,因为它包含连字符(-)