using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
namespace WangwoSoft.ShejiWorld.WebSite.GeneralHandler
{
/// <summary>
/// MemberHandler 的摘要说明
/// </summary>
public class MemberHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//要复制的表名
string table = "member_user";
//构造连接字符串
SqlConnectionStringBuilder sqlConnect1 = new SqlConnectionStringBuilder();
sqlConnect1.DataSource = "211.149.***.***";
sqlConnect1.InitialCatalog = "*****";//目标数据库
sqlConnect1.IntegratedSecurity = false;
sqlConnect1.UserID = "*****";//登录ID
sqlConnect1.Password = "*******";//数据库密码
SqlConnectionStringBuilder sqlConnect2 = new SqlConnectionStringBuilder();
sqlConnect2.DataSource = "211.149.***.***";
sqlConnect2.InitialCatalog = "*****";//源数据库
sqlConnect2.IntegratedSecurity = false;//当true的时候为windows身份验证
sqlConnect2.UserID = "*******";//登录ID
sqlConnect2.Password = "*******";//数据库密码
//调用复制数据库函数
string result = InsertTable(sqlConnect1.ConnectionString, sqlConnect2.ConnectionString, table);
context.Response.Write(result);
}
//参数为两个数据库的连接字符串
private string InsertTable(string conString1,string conString2,string tabString)
{
//连接数据库
SqlConnection conn1 = new SqlConnection();
conn1.ConnectionString = conString1;
conn1.Open();
SqlConnection conn2 = new SqlConnection();
conn2.ConnectionString = conString2;
conn2.Open();
//填充DataSet1
SqlDataAdapter adapter1 = new SqlDataAdapter("select * from "+tabString,conn1);
DataSet dataSet1 = new DataSet();
if (dataSet1!=null)
{
adapter1.Fill(dataSet1, tabString);
}
SqlDataAdapter adapter2 = new SqlDataAdapter("select * from " + tabString, conn2);
DataSet dataSet2 = new DataSet();
SqlCommand cmd2 = new SqlCommand("select COUNT(*) from "+tabString,conn2);
Object res2 = cmd2.ExecuteScalar();
if (res2!=null)
{
int nCount = Convert.ToInt32(res2.ToString());
if (nCount==0)
{
conn1.Close();
conn2.Close();
return "没有数据";
}
}
//填充DataSet2
if (dataSet2!=null)
{
adapter2.Fill(dataSet2, tabString);
}
//复制数据
for (int i = 0; i < dataSet2.Tables[0].Rows.Count; i++)
{
dataSet1.Tables[0].LoadDataRow(dataSet2.Tables[0].Rows[i].ItemArray, false);
}
//将DataSet变换显示在与其关联的目标数据库
SqlCommandBuilder cb = new SqlCommandBuilder(adapter1);
adapter1.Update(dataSet1, tabString);
cb.RefreshSchema();
return "表" + tabString + "复制成功!";
conn1.Close();
conn2.Close();
}
public bool IsReusable
{
get
{
return false;
}
}
}
}