好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > SQL2005CLR函數擴展-解析天氣服務的實現

SQL2005CLR函數擴展-解析天氣服務的實現

熱門標簽:舞鋼市地圖標注app 海南自動外呼系統價格 創業電銷機器人 電銷機器人虛擬號碼 沈陽智能外呼系統代理 松原導航地圖標注 九鹿林外呼系統怎么收費 浙江地圖標注 滄州營銷外呼系統軟件
我們可以用CLR獲取網絡服務 來顯示到數據庫自定函數的結果集中,比如163的天氣預報
http://news.163.com/xml/weather.xml
他的這個xml結果的日期是不正確的,但這個我們暫不討論。
從這個xml獲取天氣的CLR代碼如下,用WebClient訪問一下就可以了。然后通過Dom對象遍歷節點屬性返回給結果集。
--------------------------------------------------------------------------------
復制代碼 代碼如下:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections;
using System.Collections.Generic;
using Microsoft.SqlServer.Server;

public partial class UserDefinedFunctions
{

    [SqlFunction (TableDefinition = "city nvarchar(100),date nvarchar(100),general nvarchar(100),temperature nvarchar(100),wind nvarchar(100)" , Name = "GetWeather" , FillRowMethodName = "FillRow" )]
    public static IEnumerable GetWeather()
    {
        System.Collections.Generic.List Item > list = GetData();
        return list;
    }
    public static void FillRow(Object obj, out SqlString city, out SqlString date, out SqlString general, out SqlString temperature, out SqlString wind)
    {
        Item data = (Item )obj;
        city = data.city;
        date = data.date;
        general = data.general;
        temperature = data.temperature;
        wind = data.wind;
    }

    class Item
    {
        public string city;
        public string date;
        public string general;
        public string temperature;
        public string wind;
    }
    static System.Collections.Generic.List Item > GetData()
    {
        System.Collections.Generic.List Item > ret = new List Item >();
        //try
        //{

            string url = "http://news.163.com/xml/weather.xml" ;
            System.Net.WebClient wb = new System.Net.WebClient ();
            byte [] b = wb.DownloadData(url);
            string data = System.Text.Encoding .Default.GetString(b);
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument ();
            doc.LoadXml(data);

            foreach (System.Xml.XmlNode node in doc.ChildNodes[1])
            {
                string city = GetXMLAttrib(node, "name" );
                foreach (System.Xml.XmlNode subnode in node.ChildNodes)
                {
                    Item item = new Item ();
                    item.city = city;
                    item.date = GetXMLAttrib(subnode, "date" );
                    item.general = GetXMLAttrib(subnode, "general" );
                    item.temperature = GetXMLAttrib(subnode, "temperature" );
                    item.wind = GetXMLAttrib(subnode, "wind" );
                    ret.Add(item);
                }
            }

        //}
        //catch(Exception ex)
        //{
        //    SqlContext.Pipe.Send(ex.Message);
        //}
        return ret;
    }

    static string GetXMLAttrib(System.Xml.XmlNode node, string attrib)
    {
        try
        {
            return node.Attributes[attrib].Value;
        }
        catch
        {
            return string .Empty;
        }
    }
};

--------------------------------------------------------------------------------
部署這個clr函數的腳本如下
--------------------------------------------------------------------------------
復制代碼 代碼如下:

drop function dbo. xfn_GetWeather
drop   ASSEMBLY TestWeather
go
CREATE ASSEMBLY TestWeather FROM 'd:/sqlclr/TestWeather.dll' WITH PERMISSION_SET = UnSAFE;
--
go
CREATE FUNCTION dbo. xfn_GetWeather ()    
RETURNS table ( city nvarchar ( 100), date nvarchar ( 100), general nvarchar ( 100), temperature nvarchar ( 100), wind nvarchar ( 100))
AS EXTERNAL NAME TestWeather. UserDefinedFunctions. GetWeather

--------------------------------------------------------------------------------
測試函數
--------------------------------------------------------------------------------
select * from dbo. xfn_GetWeather ()

標簽:臺灣 商洛 日喀則 公主嶺 咸寧 西藏 海口 寶雞

巨人網絡通訊聲明:本文標題《SQL2005CLR函數擴展-解析天氣服務的實現》,本文關鍵詞  SQL2005CLR,函數,擴展,解析,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《SQL2005CLR函數擴展-解析天氣服務的實現》相關的同類信息!
  • 本頁收集關于SQL2005CLR函數擴展-解析天氣服務的實現的相關信息資訊供網民參考!
  • 推薦文章