Webservice数据迁移

using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;

namespace JTZFCXC
{
    ///  
    /// Service1 的摘要说明 
    ///  
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        #region
        [WebMethod]
        public void btnStart_Click(String sourceConnection)
        {
            String sourceConnectionString = sourceConnection; //kf_sj_JTZFCXC项目的数据库连接
            String destinationConnectionString = ConfigurationManager.AppSettings["connstr"].ToString();
            //WebService中的webconfig的数据库连接,即数据要保存到的数据库
            DataTable data1 = SelectDataFromSource(sourceConnectionString, "select top 1 * from jkr order by jkr_lurudatetime desc");//获取数据 CopyDataToDestinationJKR(destinationConnectionString, data1);//复制数据 
            DataTable data2 = SelectDataFromSource(sourceConnectionString, "select top 1 * from cy order by CY_code desc");//获取数据
            CopyDataToDestinationCY(destinationConnectionString, data2);//
        }
        //获取数据非常简单,它只是通过执行存储过程返回一个DataTable的查询结果集,如下: 
        public DataTable SelectDataFromSource(String connectionString, String strsql)
        {
            DataTable data = new DataTable();
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                connection.Open(); SqlCommand cmd = new SqlCommand(strsql, connection);
                SqlDataReader reader = cmd.ExecuteReader();
                data.Load(reader); connection.Close(); connection.Dispose();
            }
            return data;
        }

        public void CopyDataToDestinationJKR(String connectionString, DataTable table)
        {
            SqlBulkCopyColumnMapping mapping1 = new SqlBulkCopyColumnMapping("JKR_code", "JKR_code");
            SqlBulkCopyColumnMapping mapping2 = new SqlBulkCopyColumnMapping("JKR_isfucha", "JKR_isfucha");
            SqlBulkCopyColumnMapping mapping3 = new SqlBulkCopyColumnMapping("JKR_name", "JKR_name");
            SqlBulkCopyColumnMapping mapping4 = new SqlBulkCopyColumnMapping("JKR_cardTYPE", "JKR_cardTYPE");
            SqlBulkCopyColumnMapping mapping5 = new SqlBulkCopyColumnMapping("JKR_isTheCity", "JKR_isTheCity");
            SqlBulkCopyColumnMapping mapping6 = new SqlBulkCopyColumnMapping("JKR_sfzh", "JKR_sfzh");
            SqlBulkCopyColumnMapping mapping7 = new SqlBulkCopyColumnMapping("JKR_bankcode", "JKR_bankcode");
            SqlBulkCopyColumnMapping mapping8 = new SqlBulkCopyColumnMapping("JKR_lurudatetime", "JKR_lurudatetime");
            SqlBulkCopyColumnMapping mapping9 = new SqlBulkCopyColumnMapping("JKR_lururen", "JKR_lururen");
            SqlBulkCopyColumnMapping mapping10 = new SqlBulkCopyColumnMapping("JKR_searchstatus", "JKR_searchstatus");
            SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString);
            bulkCopy.BatchSize = 100; bulkCopy.BulkCopyTimeout = 5;
            bulkCopy.ColumnMappings.Add(mapping1);
            bulkCopy.ColumnMappings.Add(mapping2);
            bulkCopy.ColumnMappings.Add(mapping3);
            bulkCopy.ColumnMappings.Add(mapping4);
            bulkCopy.ColumnMappings.Add(mapping5);
            bulkCopy.ColumnMappings.Add(mapping6);
            bulkCopy.ColumnMappings.Add(mapping7);
            bulkCopy.ColumnMappings.Add(mapping8);
            bulkCopy.ColumnMappings.Add(mapping9);
            bulkCopy.ColumnMappings.Add(mapping10);
            bulkCopy.DestinationTableName = "jkr";
            bulkCopy.NotifyAfter = 200;
            bulkCopy.WriteToServer(table);
        }

        public void CopyDataToDestinationCY(String connectionString, DataTable table)
        {
            SqlBulkCopyColumnMapping mapping1 = new SqlBulkCopyColumnMapping("CY_code", "CY_code");
            SqlBulkCopyColumnMapping mapping2 = new SqlBulkCopyColumnMapping("CY_JKR_code", "CY_JKR_code");
            SqlBulkCopyColumnMapping mapping3 = new SqlBulkCopyColumnMapping("CY_JKR_isfucha", "CY_JKR_isfucha");
            SqlBulkCopyColumnMapping mapping4 = new SqlBulkCopyColumnMapping("CY_bankcode", "CY_bankcode");
            SqlBulkCopyColumnMapping mapping5 = new SqlBulkCopyColumnMapping("CY_name", "CY_name");
            SqlBulkCopyColumnMapping mapping6 = new SqlBulkCopyColumnMapping("CY_guanxi", "CY_guanxi");
            SqlBulkCopyColumnMapping mapping7 = new SqlBulkCopyColumnMapping("CY_isTheCity", "CY_isTheCity");
            SqlBulkCopyColumnMapping mapping8 = new SqlBulkCopyColumnMapping("CY_sfzh", "CY_sfzh");
            SqlBulkCopyColumnMapping mapping9 = new SqlBulkCopyColumnMapping("CY_cardType", "CY_cardType");
            SqlBulkCopy bulkCopy = new SqlBulkCopy(connectionString); bulkCopy.BatchSize = 100;
            bulkCopy.BulkCopyTimeout = 5;
            bulkCopy.ColumnMappings.Add(mapping1);
            bulkCopy.ColumnMappings.Add(mapping2);
            bulkCopy.ColumnMappings.Add(mapping3);
            bulkCopy.ColumnMappings.Add(mapping4);
            bulkCopy.ColumnMappings.Add(mapping5);
            bulkCopy.ColumnMappings.Add(mapping6);
            bulkCopy.ColumnMappings.Add(mapping7);
            bulkCopy.ColumnMappings.Add(mapping8);
            bulkCopy.ColumnMappings.Add(mapping9);
            bulkCopy.DestinationTableName = "cy";
            bulkCopy.NotifyAfter = 200;
            bulkCopy.WriteToServer(table);
        }
        #endregion
    }
} 

//项目引用webservice 
protected void btnOk_Click(object sender, EventArgs e) 
{ 
    com.Service1 csw = new Maticsoft.Web.com.Service1();
    try 
    {
        csw.btnStart_Click(ConfigurationManager.AppSettings["ConnectionString"].ToString()); 
        MessageBox.ResponseScript(this, "alert('保存成功!');window.location.href='Luruxinxi.aspx'");
    } 
    catch ( Exception ex) 
    { 
        MessageBox.ResponseScript(this, "保存失败!"); throw; 
    } 
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值