C#操纵大量数据表的一种方法菜鸟经验谈
2009-07-31 10:55
232 查看
问题描述:一个数据库中的某张表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)
}
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;
}
}
}
这时我们不能采用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)
}
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;
}
}
}
相关文章推荐
- c#中向ListView中添加一行数据的一种简单方法
- java中导出大量数据到Excel的一种方法
- C#下用zedGraph生成大量数据统计图表的方法(通过修改一些源码)
- PB里协调多数据窗口数据操纵的一种方法(即多Tab标签可同时保存)
- PB中协调多数据窗口数据操纵的一种解决方法
- 大量数据优化常用方法
- DWH的保留原始历史数据的一种方法
- Oracle 不同用户之间 大量数据转移的方法测试
- C# 将内存中的datatable数据导出为Excel(方法一,以文件流方式导出)
- 用数据库访问的方法解决从excel大量导入数据
- 一种客户端即时通信数据的加密和解密方法
- 关于C#代码用F12转到定义时,总是显示从元数据的解决方法
- SELECT INTO和INSERT INTO SELECT的区别 类似aaa?a=1&b=2&c=3&d=4,如何将问号以后的数据变为键值对 C# 获取一定区间的随即数 0、1两个值除随机数以外的取值方法(0、1两个值被取值的概率相等) C# MD5 加密,解密 C#中DataTable删除多条数据
- C# 各种导入 Excel 文件的数据的方法总结
- jdbc查询大量数据内存溢出的解决方法
- phpmyadmin里面导入sql语句格式的大量数据的方法
- c# 中添加数据到数据库中的三种方法 .
- 实现数据权限控制的一种方法
- C# 实现 数据加密、解密方法
- JQuery的Ajax使用Get,Post方法调用C#WebService并返回数据