using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
Init();
}
private string strConn = "server=.;uid=sa;pwd=sa;database=test";
SqlConnection sqlconn = null;
SqlDataAdapter sqlda1 = null, sqlda2 = null, sqlda3 = null, sqlda4 = null, sqlda5 = null,sqlda6=null;
DataSet ds = new DataSet();
DSSendCollection dsSend = new DSSendCollection();
//------------------------------------------------------------------------------------------//
private void Init()
{
this.btnSend.Click += new EventHandler(btnSend_Click);
this.Load += new EventHandler(Form2_Load);
}
void Form2_Load(object sender, EventArgs e)
{
using (sqlconn = new SqlConnection(strConn))
{
sqlda1 = new SqlDataAdapter("select * from maint", sqlconn);
sqlda1.Fill(ds, "MainT");
sqlda2 = new SqlDataAdapter("select * from sub1", sqlconn);
sqlda2.Fill(ds, "Sub1");
sqlda3 = new SqlDataAdapter("select * from sub2", sqlconn);
sqlda3.Fill(ds, "Sub2");
sqlda4 = new SqlDataAdapter("select * from sub3", sqlconn);
sqlda4.Fill(ds, "Sub3");
sqlda5 = new SqlDataAdapter("select * from sub4", sqlconn);
sqlda5.Fill(ds, "Sub4");
sqlda6 = new SqlDataAdapter("select * from sub5", sqlconn);
sqlda6.Fill(ds, "Sub5");
this.dsSend.Merge(ds);
this.ds.AcceptChanges();
//dsSend.WriteXml("d:\\dsSendTest.xml");
//MessageBox.Show("WriteXml成功!");
}
}
void btnSend_Click(object sender, EventArgs e)
{
SendDS(50);
}
private void ImportRow(DataRow drSource, DataSet dsSplit)
{
string tbName = drSource.Table.TableName.ToString().Trim();
if (!dsSend.Tables.Contains(tbName)) return;
dsSplit.Tables[tbName].ImportRow(drSource);
DataRelationCollection relations = drSource.Table.ChildRelations;
foreach (DataRelation relation in relations)
{
DataRow[] drChilds = drSource.GetChildRows(relation);
if (drChilds != null || drChilds.Length > 0)
{
foreach (DataRow drChild in drChilds)
{
ImportRow(drChild, dsSplit);
}
}
}
}
int iCurrent = 0;
int iPageCurrent = 1;
private void SendDS(int iPageSize)
{
int iStartPos = 0;
int iEndPos = iPageSize;
int iMaxCount = this.dsSend.MainT.Rows.Count;
int iLastPageSize = iMaxCount % iPageSize;
int iPageCount = ((iLastPageSize < iPageSize) && iLastPageSize != 0) ? iMaxCount / iPageSize + 1 : iMaxCount / iPageSize;
//DSSendCollection dsSplit = this.dsSend.Clone() as DSSendCollection;
if (dsSend.MainT.Rows.Count < iPageSize)
SendMessage(iPageCount);
else
{
//从元数据源复制记录行
for (int iStep = 0; iStep < iPageCount; iStep++)
{
DSSendCollection dsSplit=new DSSendCollection();
for (int i = iStartPos; i < iEndPos; i++)
{
/*
if (this.dsSend.MainT.Rows[i] != null)
dsSplit.MainT.ImportRow(this.dsSend.MainT.Rows[i]);//导入主表数据
//导入从表数据--Method1
int iChildTBCount = dsSend.MainT.ChildRelations.Count;
for (int j = 0; j < iChildTBCount; j++)
{
foreach (DataRow row in dsSend.MainT.ChildRelations[j].ChildTable.Select("uuid='" + dsSend.MainT.Rows[i]["UUID"].ToString().Trim() + "'"))
{
if (row != null)
dsSplit.MainT.ChildRelations[j].ChildTable.ImportRow(row);
}
}
//导入从表数据--Method2
//DataRelationCollection relation = this.dsSend.MainT.ChildRelations;
//
//int iTBCount = this.dsSend.Tables.Count;
//for (int j = 0; j < iTBCount; j++)
//{
// if (dsSend.Tables[j].TableName != "MainT")
// {
// foreach (DataRow row in this.dsSend.Tables[j].Select("uuid='" + this.dsSend.MainT.Rows[i]["uuid"].ToString().Trim() + "'"))
// {
// if (row != null)
// dsSplit.Tables[j].ImportRow(row);
// }
// }
//}
//
iCurrent++;
*/
ImportRow(dsSend.MainT.Rows[i], dsSplit);
iCurrent++;
}
iPageCurrent++;
iStartPos = iCurrent;
if (iPageCurrent == iPageCount)
iEndPos = iMaxCount;
else
iEndPos = iPageSize * iPageCurrent;
dsSplit.WriteXml("d:\\dsSplit.xml");
SendMessage(iPageCount);
dsSplit.Clear();
}
iPageCurrent = 1;
}
}
int iCall = 0;
private void SendMessage(int count)
{
iCall++;
if (iCall == count)
{
MessageBox.Show("成功发送:" + "" + count + "次");
iCall = 0;
this.Close();
}
}
}
}