c# web service 数据访问提取

近期接触到一个项目,从远程数据库服务器提取数据同步到本地数据库。这是一个基本思路。由于远程数据库包含全国各地区的数据,数据量庞大,该数据库没有开放直接访问的权限,以免造成误操作,误删除。提供了webservice访问提取数据接口。方便各地区同步提交数据。
进入准备工作,基于c#操作websercvice的便利,选用了c#编程语言。现将实现步骤呈现。 vs 中新建一项目 ,我这里为了展示便利,新建了winform工程项目,包含窗体。截图如下
这里写图片描述
在GetData项目中右键添加服务引用如图这里写图片描述
在地址处写入要访问的数据服务器地址服务。
主要代码

 private void exportData(string businessType) {

            InitTempDic(businessType);
            loadingCircleChange(businessType);
            XmlDocument inputDocument = new XmlDocument();
            string fileName = filePath + "tdjg.xml";
            inputDocument.Load(fileName);
            InitRequestXml(inputDocument, fileName);
            GisqStandard getDataWebService = new GetData.GisqStandard();

            string dir = filePath + tempDictionary["BusinessType"] + "(" + businessTypeDictionary[tempDictionary["BusinessType"]] + ")\\";
            if (Directory.Exists(dir))
            {
                Directory.Delete(dir, true);
            }
            Directory.CreateDirectory(dir);

            string dataFromServer = string.Empty;

            while (true)
            {
                try
                {
                    dataFromServer = getDataWebService.AnalyzeRequest("user", "password", inputDocument.OuterXml);
                    string filePath = dir + "tdjg-" + currentPage.ToString() + ".xml";
                    bool status = WriteFileMemory(getMemoryStream(dataFromServer.Trim()), filePath);//以字符流的形式写入
                    if (!status)
                    {
                        MessageBox.Show("没有数据!");
                    }
                    if (checkData(dataFromServer))
                    {
                        currentPage++;
                        setCurrentPage(currentPage.ToString(), inputDocument, fileName);
                    }
                    else
                    {
                        break;
                    }
                }
                catch
                {
                    WriteLogToFile(currentPage.ToString());
                    setCurrentPage(currentPage.ToString(), inputDocument, fileName);
                }
            }

        }

代码中 关键 部分 就是 while true 监听 获取数据。

private StringDictionary businessTypeDictionary = new StringDictionary();
dictionary的数据类型应用

        private void InitBusinessDict()
        {
            businessTypeDictionary.Add("32", "土地转让信息");
            businessTypeDictionary.Add("33", "土地出租信息");
            businessTypeDictionary.Add("41", "集体建设用地信息");
            businessTypeDictionary.Add("51", "合同清理");
            businessTypeDictionary.Add("52", "划拨专项清理");
        }

代码参照
从服务器返回的数据格式 是 xml 例子如下

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Response><Data><PRI_TABLE_DATA tableName="xcvd" 
queryFields="*">
<RECORD DGYJ_ID="303" 
GD_GUID="c5" 
XZQ_DM="jkkj'" 
XM_MC="iii"
 BH="8888888" 
 DZ_BA_BH="666666" 
 ZD_BH="9999999" 
 GD_ZMJ="9999"
  GY_FS="3"
   TD_YT="073"
    PZ_WH="888" 
    GY_MJ="1.37033" 
    JE="53.65" 
    JD_SJ="2010/11/30 0:00:00"
     DG_SJ="2012/6/18 0:00:00" 
     QD_RQ="2010/11/29 15:39:12" 
     XM_ZT="211" 
     CREATE_USER="jjj" 
     WL_BZ="1" 
     YJ_CREATE_DATE="2012/6/19 2:30:02" 
     YJ_CZ_DATE="" 
     YJ_CZ_USER="" 
     YJ_CZ_FS="" 
     YJ_CZ_YJ="" 
     YJ_ZT="0" 
     YJ_DUB_CS="1" 
     YJ_WWF_YY="" 
     WWF_YY_SM=""
 MODIFY_DATE="2017/5/18 13:44:13" />

解析xml 方法: XmlDocument xmlDoc = new XmlDocument();
XmlNodeList nodeList_pri = xmlDoc.SelectNodes("//PRI_TABLE_DATA");
if (nodeList[i].Attributes["tableName"] != null)
{
table_Name = nodeList[i].Attributes["tableName"].Value.Trim();
}

猜你喜欢

转载自blog.csdn.net/guoruijun_2012_4/article/details/79292091
今日推荐