SQL2005CLR函數(shù)擴(kuò)展-解析天氣服務(wù)的實(shí)現(xiàn)
http://news.163.com/xml/weather.xml
他的這個(gè)xml結(jié)果的日期是不正確的,但這個(gè)我們暫不討論。
從這個(gè)xml獲取天氣的CLR代碼如下,用WebClient訪問(wèn)一下就可以了。然后通過(guò)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 ()

相關(guān)文章
sql2005 本地計(jì)算機(jī)上的SQL SERVER服務(wù)啟動(dòng)后又停止了解決方法
這篇文章主要介紹了本地計(jì)算機(jī)上的SQL SERVER服務(wù)啟動(dòng)后又停止了解決方法,需要的朋友可以參考下2015-01-01
sqlserver FOR XML PATH 語(yǔ)句的應(yīng)用
大家都知道在SQL Server中利用 FOR XML PATH 語(yǔ)句能夠把查詢(xún)的數(shù)據(jù)生成XML數(shù)據(jù),下面是它的一些應(yīng)用示例。2010-05-05
SQL Server CROSS APPLY和OUTER APPLY的應(yīng)用詳解
SQL Server數(shù)據(jù)庫(kù)操作中,在2005以上的版本新增加了一個(gè)APPLY表運(yùn)算符的功能2011-10-10
SQL2008中通過(guò)DBCC OPENTRAN和會(huì)話(huà)查詢(xún)事務(wù)
無(wú)論是有意無(wú)意,如果事務(wù)在數(shù)據(jù)庫(kù)中保持打開(kāi),則它會(huì)阻塞其他進(jìn)程對(duì)修改后的數(shù)據(jù)進(jìn)行操作。2011-06-06
SQLServer 2008中通過(guò)DBCC OPENTRAN和會(huì)話(huà)查詢(xún)事務(wù)
無(wú)論是有意無(wú)意,如果事務(wù)在數(shù)據(jù)庫(kù)中保持打開(kāi),則它會(huì)阻塞其他進(jìn)程對(duì)修改后的數(shù)據(jù)進(jìn)行操作。同樣,對(duì)事務(wù)日志進(jìn)行備份也只會(huì)截?cái)嗖换顒?dòng)事務(wù)的那部分事務(wù)日志,所以打開(kāi)的事務(wù)會(huì)導(dǎo)致日志變多(甚至達(dá)到物理限制),直到事務(wù)被提交或回滾。2011-05-05
SQL2008中SQL應(yīng)用之-鎖定(locking) 應(yīng)用分析
鎖定(Locking)是一個(gè)關(guān)系型數(shù)據(jù)庫(kù)系統(tǒng)的常規(guī)和必要的一部分,它防止對(duì)相同數(shù)據(jù)作 并發(fā)更新 或在更新過(guò)程中查看數(shù)據(jù), 從而保證被更新數(shù)據(jù)的完整性。它也能防止用戶(hù)讀取正在被修改的數(shù)據(jù) 。2011-06-06
SQL2005 provider: 命名管道提供程序 error: 40 無(wú)法打開(kāi)到 SQL Server 的連接
這篇文章主要介紹了SQL2005 provider: 命名管道提供程序 error: 40 無(wú)法打開(kāi)到 SQL Server 的連接,需要的朋友可以參考下2015-01-01

