您的位置:澳门402永利com > 编程应用 > 简单代码转移数据库数据

简单代码转移数据库数据

发布时间:2019-09-23 20:45编辑:编程应用浏览(136)

    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;
    }

    //参数为三个数据库的接连字符串
    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.Update(dataSet1, tabString);
    cb.RefreshSchema();

    return "表" + tabString + "复制作而成功!";
    conn1.Close();
    conn2.Close();
    }

    public bool IsReusable
    {
    get
    {
    return false;
    }
    }
    }
    }

    本文由澳门402永利com发布于编程应用,转载请注明出处:简单代码转移数据库数据

    关键词: