zoukankan      html  css  js  c++  java
  • C#:从海量数据表中读取每一条记录,并处理该记录记录,储存到另一个表中的方案。



    问题描述:一个数据库中的某张表A有5,6万条记录。我们要对该表的每条记录做相同的处理,并将处理结果保存到表B。
    这时我们不能采用SqlDataReader读一条记录,处理,储存到表B的方案。因为sqldatareader对数据库连接具有独占性,所以我们就不能储存记录到表B了。还有一种方案是可行的,就是在内存中采用datatable数据结构映射一张A的虚表A‘,这种方法固然可行。但是这样做无疑是耗内存的。于是我们采取以下方案(这里的表Article就对应于上文抽象模型中的表A)
          DataBaseManipulation dbm=new DataBaseManipulation();
                string conStr = "server=(local);database=ArticleCollection;uid=sa;pwd=123456";
                string table = "Article";
                SqlConnection conn = new SqlConnection(conStr);//建立连接
              

    int ArticleLastIndex = dbm.GetTableLastIndex(table, conn, "ArticleId");
                int lastindex = 0;

                while (lastindex < ArticleLastIndex)
                {
                    string select = string.Format("select top 100 ArticleId,AuthorString,WorkPlaceString From Article  Where ArticleId>{0} ORDER BY ArticleId", lastindex);
                    SqlDataAdapter adapter = new SqlDataAdapter(select, conn);
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                 
                   int lastAuthorIndex = dbm.GetTableLastIndex("Author", conn, "AuthorId");

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        //处理每条记录
                       .........................................................
                      //将每条记录处理的结果保存至Author表中(Author表相当于上文抽象建模中的表B)

                    }
                    int lastindex = int.Parse(dt.Rows[dt.Rows.Count - 1]["ArticleId"].ToString());
                }

    数据库操纵类如下

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;

    namespace CreateAuthorEntity
    {
        class DataBaseManipulation
        {
            public int GetDataBaseTableCount(string table, SqlConnection conn, string des)
            {
                string findCount = string.Format("SELECT COUNT({0})  FROM {1}", des, table);
                conn.Open();
                SqlCommand command = new SqlCommand(findCount, conn);
                SqlDataReader reader = command.ExecuteReader();
                reader.Read();
                int count = reader.GetInt32(0);
                reader.Close();
                conn.Close();
                return count;

            }
            public int GetTableLastIndex(string table, SqlConnection conn, string des)
            {
                int lastIndex;
                string findLastIndex = string.Format("SELECT MAX({0}),COUNT({0}) FROM {1}", des, table);
                conn.Open();
                SqlCommand command = new SqlCommand(findLastIndex, conn);
                SqlDataReader reader = command.ExecuteReader();
                reader.Read();
                if (reader.GetInt32(1) == 0)
                {
                    lastIndex = 0;
                }
                else
                {
                    lastIndex = reader.GetInt32(0);
                }
                reader.Close();
                conn.Close();
                return lastIndex;


            }
        }
    }



  • 相关阅读:
    074孤荷凌寒从零开始学区块链第74天DAPP029
    073孤荷凌寒从零开始学区块链第73天DAPP028
    iOS开发——UI进阶篇(十七)CALayer,核心动画基本使用
    iOS开发——UI进阶篇(十六)Quartz2D实战小例子
    iOS开发——UI进阶篇(十五)Quartz2D介绍
    iOS开发——UI进阶篇(十四)modal
    iOS开发——UI进阶篇(十三)UITabBarController简单使用,qq主流框架
    iOS开发——UI进阶篇(十二)事件处理,触摸事件,UITouch,UIEvent,响应者链条,手势识别
    iOS开发——UI进阶篇(十一)应用沙盒,归档,解档,偏好设置,plist存储,NSData,自定义对象归档解档
    iOS开发——UI进阶篇(十)导航控制器、微博详情页、控制器的View的生命周期
  • 原文地址:https://www.cnblogs.com/finallyliuyu/p/1532280.html
Copyright © 2011-2022 走看看