问题描述:一个数据库中的某张表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;


        }
    }
}



posted on 2009-07-28 09:59  finallyly  阅读(3493)  评论(2编辑  收藏  举报