【交流】SQL2005CLR函数扩展-天气服务

jinjazz 2009-05-15 10:35:55
加精
Blog原文:
http://blog.csdn.net/jinjazz/archive/2009/05/14/4187051.aspx

我们可以用CLR获取网络服务 来显示到数据库自定函数的结果集中,比如163的天气预报
http://news.163.com/xml/weather.xml

最终效果
select * from dbo.xfn_GetWeather ()



只要你知道如何用WebClient下载数据并分析xml,如何用IEnumerable写表值函数就行了。

clr代码

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;
}
}
};


部署代码

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

...全文
753 65 打赏 收藏 转发到动态 举报
写回复
用AI写文章
65 条回复
切换为时间正序
请发表友善的回复…
发表回复
joyhen 2010-12-14
  • 打赏
  • 举报
回复
牛B啊,楼主你好强大,三花聚顶我顶
  • 打赏
  • 举报
回复
学习...
luyuwei2008 2010-11-30
  • 打赏
  • 举报
回复
学习了
lfywy 2010-05-13
  • 打赏
  • 举报
回复
不错~~~~
lovezx1028 2009-12-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wufeng4552 的回复:]
頂你個肺:)
[/Quote] 这孩子学到了哦。
csdn_风中雪狼 2009-05-19
  • 打赏
  • 举报
回复
支持
鱼大洋要学习 2009-05-18
  • 打赏
  • 举报
回复
学习了。项一下
lingyun617 2009-05-18
  • 打赏
  • 举报
回复
学习一下!
teardemon 2009-05-18
  • 打赏
  • 举报
回复
学习,好好学习...
hk_no1 2009-05-18
  • 打赏
  • 举报
回复
准备从事数据库方向了。好好学习,向高手学习。
conan304 2009-05-18
  • 打赏
  • 举报
回复
看看。好帖。
zd123456zd123456 2009-05-18
  • 打赏
  • 举报
回复
顶顶
SlaughtChen 2009-05-18
  • 打赏
  • 举报
回复
顶一个 ,谢谢
claro 2009-05-17
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 HEROWANG 的回复:]
顶剪剪
[/Quote]
lijie9693 2009-05-17
  • 打赏
  • 举报
回复
学习ing...
mlott 2009-05-17
  • 打赏
  • 举报
回复
nb
跃天虎 2009-05-17
  • 打赏
  • 举报
回复
顶,学习
Teng_s2000 2009-05-17
  • 打赏
  • 举报
回复
mark
kk324 2009-05-17
  • 打赏
  • 举报
回复
看下 学习下
dk503031240 2009-05-17
  • 打赏
  • 举报
回复
向高手学习学习
加载更多回复(42)

34,591

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧