SQL2005CLR函數(shù)擴(kuò)展-解析天氣服務(wù)的實(shí)現(xiàn)
更新時(shí)間:2013年06月27日 09:40:42 作者:
其實(shí)我們可以用CLR獲取網(wǎng)絡(luò)服務(wù),來顯示到數(shù)據(jù)庫自定函數(shù)的結(jié)果集中,比如163的天氣預(yù)報(bào)。需要的朋友參考下
我們可以用CLR獲取網(wǎng)絡(luò)服務(wù) 來顯示到數(shù)據(jù)庫自定函數(shù)的結(jié)果集中,比如163的天氣預(yù)報(bào)
http://news.163.com/xml/weather.xml
他的這個(gè)xml結(jié)果的日期是不正確的,但這個(gè)我們暫不討論。
從這個(gè)xml獲取天氣的CLR代碼如下,用WebClient訪問一下就可以了。然后通過Dom對(duì)象遍歷節(jié)點(diǎn)屬性返回給結(jié)果集。
--------------------------------------------------------------------------------
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;
}
}
};
--------------------------------------------------------------------------------
部署這個(gè)clr函數(shù)的腳本如下
--------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------
測(cè)試函數(shù)
--------------------------------------------------------------------------------
select * from dbo. xfn_GetWeather ()

http://news.163.com/xml/weather.xml
他的這個(gè)xml結(jié)果的日期是不正確的,但這個(gè)我們暫不討論。
從這個(gè)xml獲取天氣的CLR代碼如下,用WebClient訪問一下就可以了。然后通過Dom對(duì)象遍歷節(jié)點(diǎn)屬性返回給結(jié)果集。
--------------------------------------------------------------------------------
復(fù)制代碼 代碼如下:
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;
}
}
};
--------------------------------------------------------------------------------
部署這個(gè)clr函數(shù)的腳本如下
--------------------------------------------------------------------------------
復(fù)制代碼 代碼如下:
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
--------------------------------------------------------------------------------
測(cè)試函數(shù)
--------------------------------------------------------------------------------
select * from dbo. xfn_GetWeather ()

相關(guān)文章
SQLServer無法打開用戶默認(rèn)數(shù)據(jù)庫 登錄失敗錯(cuò)誤4064的解決方法
這篇文章主要介紹了SQLServer無法打開用戶默認(rèn)數(shù)據(jù)庫 登錄失敗錯(cuò)誤4064的解決方法,需要的朋友可以參考下2015-01-01Sql Server 2005中查詢用分隔符分割的內(nèi)容中是否包含其中一個(gè)內(nèi)容
Sql Server 2005中查詢用分隔符分割的內(nèi)容中是否包含其中一個(gè)內(nèi)容,多個(gè)朋友給予的參考。2011-10-10Sql Server安裝出錯(cuò),安裝程序配置服務(wù)器失敗的解決方法小結(jié)
機(jī)子之前有裝了sql server 2000,而且可以正常使用,最近突然無法連接到本地?cái)?shù)據(jù)庫,打算重裝,結(jié)果重裝時(shí)出現(xiàn)了無法安裝完畢的問題,一下總結(jié)了重裝時(shí)需要注意的問題,以及常見錯(cuò)誤的解決方法2012-10-10SQL Server 2005基礎(chǔ)知識(shí)詳細(xì)整理
以下是小編對(duì)SQL Server 2005中的基礎(chǔ)知識(shí)進(jìn)行了詳細(xì)的綜合整理。需要的朋友可以過來參考下2013-08-08SqlServer 2005的排名函數(shù)使用小結(jié)
Sql Server 2005 引入了4個(gè)新的排名函數(shù):ROW_NUMBER、BANK、DENSE_RANK和NTILE。2010-04-04