代码改变世界

利用 HttpRequest 得到 GzipStream ,从而获取远程服务器数据库中的数据

2011-11-20 18:06  音乐让我说  阅读(408)  评论(0编辑  收藏  举报

一般远程服务器上放一个导出(Export.aspx)页面,本地服务器上放一个导入(Import.aspx)页面,以实现从远程服务器上获取数据。

直接贴代码了:

Export.aspx

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.IO.Compression;

namespace WebDownloadGzipStream
{
    public partial class _Export : System.Web.UI.Page
    {

        private static readonly string ConnString = ConfigurationManager.ConnectionStrings["exportConnString"].ConnectionString;

        private static readonly string ExportPassword = ConfigurationManager.AppSettings["exportPassword"];

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BeginExport();
            }
        }

        protected void BeginExport()
        {
            string tableName = Request.QueryString["tableName"];
            //这里要验证表是否为空、是否存在,来一个密码更好
            string password = Request.QueryString["password"];
            if (password != ExportPassword)
            {
                return;
            }
            DataSet ds = new DataSet();
            using (SqlConnection conn = new SqlConnection(ConnString))
            {
                SqlDataAdapter adapter = new SqlDataAdapter("select * from " + tableName, conn);
                adapter.Fill(ds);
            }
            using (MemoryStream memoryStream = new MemoryStream())
            {
                ds.WriteXml(memoryStream,XmlWriteMode.WriteSchema);
                memoryStream.Seek(0,SeekOrigin.Begin);
                using(GZipStream gzip = new GZipStream(Response.OutputStream, CompressionMode.Compress))
                {
                    CopyStream(memoryStream, gzip);
                }
            }
        }

        public static void CopyStream(Stream source, Stream target)
        {
            byte[] buffer = new byte[4096];
            int count = source.Read(buffer, 0, 4096);
            while(count > 0)
            {
                target.Write(buffer, 0, count);
                count = source.Read(buffer,0, 4096);
            }
        }
    }
}

 

Import.aspx

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.IO.Compression;
using System.Net;

namespace WebDownloadGzipStream
{
    public partial class Import : System.Web.UI.Page
    {
        private static readonly string ConnString = ConfigurationManager.ConnectionStrings["importConnString"].ConnectionString;

        private static readonly string RemoteServerExportUrl = ConfigurationManager.AppSettings["remoteServerExportUrl"];

        protected void btnImport_Click(object sender, EventArgs e)
        {
            string[] tableNames = new string[]
            {
                "aaa","bbb"
            };
            foreach (var item in tableNames)
            {
                BeginImport(item);
            }
        }

        protected void BeginImport(string tableName)
        {
            DataSet ds = GetDataSetFromRemoteServer(tableName);
            using (SqlConnection conn = new SqlConnection(ConnString))
            {
                conn.Open();
                using(SqlBulkCopy bulkCopy = new SqlBulkCopy(conn))
                {
                    bulkCopy.DestinationTableName = tableName;
                    bulkCopy.WriteToServer(ds.Tables[0], DataRowState.Unchanged);
                }
            }
        }

        protected DataSet GetDataSetFromRemoteServer(string tableName)
        {
            HttpWebRequest wr = WebRequest.Create(RemoteServerExportUrl) as HttpWebRequest;
            using(Stream s = (wr.GetResponse() as HttpWebResponse).GetResponseStream())
            {
                using(GZipStream gzip = new GZipStream(s,CompressionMode.Decompress))
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(gzip);
                    ds.AcceptChanges();
                    return ds;
                }
            }
        }
    }
}

 

谢谢浏览!