Home > Database > Mysql Tutorial > body text

MySQL多表循环遍历更新

WBOY
Release: 2016-06-07 17:29:36
Original
1115 people have browsed it

MySql数据库中存在大量的表结构,而且这些表都存在一个共同点,就是表中都有相同字段,比如id,name,city,adress,lat,lng。表中字

MySql数据库中存在大量的表结构,而且这些表都存在一个共同点,就是表中都有相同字段,比如id,name,city,adress,lat,lng。表中字段id,name,city,adress不允许为空,所以表中这几个字段的数据都是已经赋给实值的,而lat与lng(经纬度)是为空的。现在要写一个程序,根据city,adress请求获取数据,把循环每一表中根据city,adress请求获取的数据更新到相应表,相应表中对应的那一行数据。这句话,可能你有点晕,说白了就是,先循环查询遍历表中city与adress的值,根据city,adress的值请求获取数据,更新到相应行中,这个表循环更新完了就跳到下个表中再循环更新,以此类推,直到全部更新。。。
 
这是我们做的一个程序,当时我还天真的以为手动输入city和adress请求获取更新,谁不曾料想数据库表中的数据有好几万条,要是手动累死你吧,呵呵。。。所以这个程序思想也是从我们同事那获取到的,很有用,我也是在不断学习当中,发表出来供大家参考,,资源共享才能更好的进步!!!
 
MySql数据库的连接字符串是:
 
  string MySqlString = "Host=ip地址;Port=端口号;User id=用户名;pwd=密码;Database=数据库名;Character Set=utf8"
 
第一步,
 
连接数据库,写个根据city,adress请求获取数据的查询方法,传个参数(数据库的表名),如Get_Data(string tablename)
 
  查询语句:string MySqlSelect = "select id,city,address,lat,lng from " + tablename;然后调用ExecuteReader()方法将值传出去,注意:表中这几个字段应该先建立一个model类(这里不细说,大家都懂,就是get{}set{}),方便传值
 

 

?

 

 

using (MySqlDataReader dataReader = Command.ExecuteReader())

  {

      while (dataReader.Read())

      {

          DataModel dataModel = new DataModel();

          dataModel.Id = dataReader["id"].ToString();

          dataModel.City = (string)dataReader["city"];

          dataModel.Address = (string)dataReader["address"];

          if (dataReader["latitude"] != DBNull.Value)

          {

              dataModel.Latitude = (decimal)dataReader["latitude"];

          }

          else

              dataModel.Latitude = 0.0M;

          if (dataReader["longitude"] != DBNull.Value)

          {

              dataModel.Latitude = (decimal)dataReader["longitude"];

          }

          else

              dataModel.Latitude = 0.0M;

          dataSource.Add(dataModel);

      }

      dataReader.Close();

      dataReader.Dispose();

  }


第二步,
 
那就是更新方法了,从查询到的数据,循环遍历表名更新。更新方法中有四个参数,根据city,adress查询出来的id,数据库的表名,以及要更新的字段lat,lng。如Update_Data(string id, string dataTable, decimal lat, decimal lng)
 
  更新语句:string MySqlUpdata = "update " + dataTable + " set latitude=" + lat + ",longitude=" + lng + " whereA";

tableName[1] = "B";

tableName[2] = "C";

tableName[3] = "D";

tableName[4] = "E";

tableName[5] = "F";

tableName[6] = "G";

tableName[7] = "H";

tableName[8] = "I";

tableName[9] = "J";


而后,把表名写个for循环,实例化上面让写的那个Model类,调用第一步里面的查询方法,将取得的实体值存在List泛型集合当中。再foreach遍历List泛型集合,取得变量参数的值(lat/lng),调用更新方法,循环更新,就行了。。。代码示例如下:
 

 

?

 

 

for (i = 0; i

 {

    List DataSource = new List();

    int h = 0;

    DataSource = Get_Data(tableName[i]);

 

    foreach (DataModel dm in DataSource)

    {

       

        try

        {

                Update_Data(dm.Id, tableName[i], dm.Latitude, dm.Longitude);

                Console.WriteLine("第" + h + "条记录以更新");

                h++;

             

        }

        catch (Exception ex)

        {

            continue;

        }

    }

    Console.WriteLine(tableName[i] + "此表已更新完毕!!!");


到此,这篇文章算是写完了,我不知道正在阅读的您会不会看的不太明白,我也不知道自己表述的如何,只知道自己能理解,呵呵。。。。
 
反正这也是资源共享,给需要的您提供一点思路即可。
 
一家之言,仅供参考!!! 

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!