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; 
    } 
} 
posted @ 2011-02-17 22:18  mile  阅读(1102)  评论(0编辑  收藏  举报