web端

  • 866
  • 0
  • 2012-03-12

摘要:web端


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data.OleDb;
using System.Data;

namespace WH_Proj
{
    /// 
    /// WH_Proj 的摘要描述
    /// 
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允許使用 ASP.NET AJAX 從指令碼呼叫此 Web 服務,請取消註解下一行。
    // [System.Web.Script.Services.ScriptService]
    public class WH_Proj : System.Web.Services.WebService
    {

        [WebMethod]
        public string myTest(string pwd, string ds, string sql)
        {
            ClickOnceTest.WebServiceEncodeLib cs = new ClickOnceTest.WebServiceEncodeLib();
            
            string s = cs.Compress(sql);            
            string str = getDataSet(pwd,ds,s);
            string rtn = cs.Decompress(str);
            return rtn;          
            
        }

        [WebMethod]
        public string ExecSql(string pwd,string DataSource, string EncodeSQL) {
            if (pwd!="2717xxxx")
            {
                return "";
            }

            try
            {
                ClickOnceTest.WebServiceEncodeLib cs = new ClickOnceTest.WebServiceEncodeLib();
                string sSQL = cs.Decompress(EncodeSQL);

                string sConnStr = ConfigurationManager.ConnectionStrings[DataSource].ConnectionString;             
                OleDbConnection conn = new OleDbConnection(sConnStr);
                conn.Open();

                OleDbCommand cmd = new OleDbCommand(sSQL, conn);
                string rtn = cmd.ExecuteNonQuery().ToString();

                cmd.Dispose();
                conn.Close();

                return rtn;
            }
            catch (Exception e)
            {
                return e.Message;                
            }            
        }

        [WebMethod]
        public string getDataSet(string pwd, string DataSource, string EncodeSQL)
        {/*
            string cs_sqlstring = ConfigurationManager.AppSettings["SAVE_EFFICACY_DATA"];
	    
            if ((DataSource == "xxxxx") && (cs_sqlstring=="TRUE"))
            {
                return getDataSetTest(pwd, DataSource, EncodeSQL);
            }
            else
            {
                return getDataSetLIVE(pwd, DataSource, EncodeSQL);
            }
	*/	
		return getDataSetLIVE(pwd, DataSource, EncodeSQL);
        }
/*
        private string getDataSetTest(string pwd, string DataSource, string EncodeSQL)
        {
            DateTime Rec_Time = DateTime.Now; //接收時間
            int Rec_Size = EncodeSQL.Length; //接收資料大小

            if (pwd != "2717xxxx")
            {
                return "";
            }

            try
            {
                string sConnStr = ConfigurationManager.ConnectionStrings[DataSource].ConnectionString;

                OleDbConnection conn = new OleDbConnection(sConnStr);

                ClickOnceTest.WebServiceEncodeLib cs = new ClickOnceTest.WebServiceEncodeLib();

                string sSQL = cs.Decompress(EncodeSQL);

                int rec_unzip_size = sSQL.Length; // 接收資料解壓縮後大小

                conn.Open();

                DateTime START_QUERY = DateTime.Now; //效能統計用

                OleDbDataAdapter da = new OleDbDataAdapter(sSQL, conn);

                DateTime END_QUERY = DateTime.Now;//效能統計用

                DataSet ds = new DataSet();

                da.Fill(ds);

                DateTime FILL_DATASET = DateTime.Now;//效能統計用

                string s = ds.Tables[0].TableName;
                System.IO.MemoryStream sm = new System.IO.MemoryStream();
                ds.WriteXml(sm, XmlWriteMode.WriteSchema); //產生XML stream

                DateTime CHANGE_XML = DateTime.Now;//效能統計用

                sm.Position = 0;
                System.IO.StreamReader reader = new System.IO.StreamReader(sm);
                string text = reader.ReadToEnd();

                int RTN_SIZE = text.Length;//資料大小

                string rtn = cs.Compress(text);  //壓縮後編碼轉成字串

                DateTime COMPRESS = DateTime.Now;//效能統計用

                int RTN_ZIP_SIZE = rtn.Length;//壓完大小

                ds.Dispose();
                da.Dispose();

                DateTime Rtn_Time = DateTime.Now; //最後資料發出時間


                string query_time = (END_QUERY - START_QUERY).Seconds.ToString() +"."+ (END_QUERY - START_QUERY).Milliseconds.ToString();
                string fill_ds_time = (FILL_DATASET - END_QUERY).Seconds.ToString() +"."+ (FILL_DATASET - END_QUERY).Milliseconds.ToString();
                string to_xml_time = (CHANGE_XML - FILL_DATASET).Seconds.ToString() +"."+ (CHANGE_XML - FILL_DATASET).Milliseconds.ToString();
                string zip_time = (Rtn_Time - CHANGE_XML).Seconds.ToString() +"."+ (Rtn_Time - CHANGE_XML).Milliseconds.ToString();

                sSQL = "INSERT INTO web_service_efficacy (REC_TIME, REC_SIZE , Rec_Unzip_Size , Rtn_Time , Rtn_Size , Rtn_Zip_Size,"
                       + " query_time,fill_ds_time,to_xml_time,zip_time)values "
                       + " ( TO_DATE('" + Rec_Time.ToString("yyyyMMddHHmmss") + "','yyyymmddHH24MISS') ," + Rec_Size.ToString()
                       + " , " + rec_unzip_size.ToString() + " , TO_DATE('" + Rtn_Time.ToString("yyyyMMddHHmmss") + "','yyyymmddHH24MISS')"
                       + " , " + RTN_SIZE.ToString() + " ," + RTN_ZIP_SIZE.ToString()
                       + "," + query_time + "," + fill_ds_time + "," + to_xml_time + "," + zip_time + " )";

                OleDbCommand cmd = new OleDbCommand(sSQL, conn);
                cmd.ExecuteNonQuery().ToString();
                cmd.Dispose();

                conn.Close();

                return rtn;
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
*/
        private string getDataSetLIVE(string pwd, string DataSource, string EncodeSQL)
        {
            if (pwd != "2717xxxx")
            {
                return "";
            }

            try
            {
                string sConnStr = ConfigurationManager.ConnectionStrings[DataSource].ConnectionString;
                OleDbConnection conn = new OleDbConnection(sConnStr);
                ClickOnceTest.WebServiceEncodeLib cs = new ClickOnceTest.WebServiceEncodeLib();
                string sSQL = cs.Decompress(EncodeSQL);
                conn.Open();
                OleDbDataAdapter da = new OleDbDataAdapter(sSQL, conn);
                DataSet ds = new DataSet();
                da.Fill(ds);
                string s = ds.Tables[0].TableName;
                System.IO.MemoryStream sm = new System.IO.MemoryStream();
                ds.WriteXml(sm, XmlWriteMode.WriteSchema); //產生XML stream
                sm.Position = 0;
                System.IO.StreamReader reader = new System.IO.StreamReader(sm);
                string text = reader.ReadToEnd();
                string rtn = cs.Compress(text);  //壓縮後編碼轉成字串
                ds.Dispose();
                da.Dispose();
                conn.Close();
                return rtn;
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }


    }
}