创建ASP.NET联合订阅与SqlConnection和VB.NET [英] Create ASP.NET Syndication Feeds with SqlConnection and VB.NET

查看:113
本文介绍了创建ASP.NET联合订阅与SqlConnection和VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是斯科特·米切尔一个伟大的文章在 ASP.NET 3.5 创建联合供稿。我的问题是,它使用C#和LINQ,这我不是在当前时间一样锋利。

This seems to be a great article by Scott Mitchell for creating syndicated feeds in ASP.NET 3.5. The problem for me is that it uses C# and Linq, which I'm not as sharp on at the current time.

<一个href=\"http://dotnetslackers.com/articles/aspnet/How-to-create-a-syndication-feed-for-your-website.aspx\" rel=\"nofollow\">http://dotnetslackers.com/articles/aspnet/How-to-create-a-syndication-feed-for-your-website.aspx

有谁知道在哪里的例子可能存在可以使用产生喜欢这篇文章银团饲料 System.ServiceModel.Syndication 命名空间的 VB.NET 的SQLConnection 对象?

Does anyone know where an example might exist for the System.ServiceModel.Syndication namespace that can produce a syndicated feed like this article using VB.NET and a SQLConnection object?

我环顾四周,每一个例子似乎在C#和LINQ(这可能是证明了我需要学习他们很快,而不是更高版本)中产生。

I've looked around and every example seems to be produced in C# and Linq (which is probably a testament to my need to learn them soon rather than later).

推荐答案

您可能已经搞明白了现在,但这里的完整性的实现以及一些VB的爱(和对的Necromancer徽章。)

You’ve probably figured it out by now, but here’s an implementation for completeness and some VB love (and an attempt for the Necromancer badge. :)

aspx页面很简单,注意60秒的缓存:

The aspx page is simple, note the 60 second cache:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="YourProject._Default" %>
<%@ OutputCache Duration="60" VaryByParam="Type" %>

您可能要考虑使用一个HttpHandler代替,但这样会工作得很好了。

You probably want to consider using an HttpHandler instead, but this will work just fine too.

在code背后:

Imports System.ServiceModel.Syndication
Imports System.Xml

Partial Public Class _Default
  Inherits System.Web.UI.Page

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim dbConn As String = "[your db connection]"
    Dim format As FeedFormats = GetFeedFormat()
    Dim posts As New List(Of SyndicationItem)

    Using cnn As New SqlClient.SqlConnection(dbConn)
      cnn.Open()

      Using cmd As New SqlClient.SqlCommand("SELECT ID, Title, Text, Url, Created FROM Posts", cnn)
        Dim reader As SqlClient.SqlDataReader = cmd.ExecuteReader

        While reader.Read
          Dim item As New SyndicationItem(reader.Item("Title"), reader("Text"), New Uri(reader("Url")))

          posts.Add(item)
        End While
      End Using
    End Using

    Dim feed As New SyndicationFeed("Your feed title", "Your feed description", New Uri("http://yourdomain.com"), posts)

    Using feedWriter As XmlWriter = XmlWriter.Create(Response.OutputStream)
      Select Case format
        Case FeedFormats.Atom
          Response.ContentType = "application/rss+xml"

          Dim atomFormatter As New Atom10FeedFormatter(feed)
          atomFormatter.WriteTo(feedWriter)
        Case FeedFormats.Rss
          Response.ContentType = "application/atom+xml"

          Dim rssFormatter As New Rss20FeedFormatter(feed)
          rssFormatter.WriteTo(feedWriter)
      End Select
    End Using
  End Sub

  Private Function GetFeedFormat() As FeedFormats
    If Request.QueryString("format") = "atom" Then
      Return FeedFormats.Atom
    Else
      Return FeedFormats.Rss
    End If
  End Function

  Public Enum FeedFormats
    Rss = 1
    Atom = 2
  End Enum
End Class

最后,对于超完整性,用于创建表的SQL脚本:

Finally, for super-completeness, the SQL script for creating the table:

CREATE TABLE [dbo].[Posts](
 [ID] [int] IDENTITY(1,1) NOT NULL,
 [Title] [nvarchar](50) NOT NULL,
 [Text] [ntext] NOT NULL,
 [Url] [nvarchar](50) NOT NULL,
 [Created] [datetime2](7) NOT NULL,
 CONSTRAINT [PK_Posts] PRIMARY KEY CLUSTERED 
(
 [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

ALTER TABLE [dbo].[Posts] ADD  CONSTRAINT [DF_Posts_Url]  DEFAULT ('') FOR [Url]
GO

ALTER TABLE [dbo].[Posts] ADD  CONSTRAINT [DF_Posts_Created]  DEFAULT (getdate()) FOR [Created]
GO

完成。 VB.NET,SQL连接,在 System.ServiceModel.Syndication 命名空间,并没有LINQ。 :)

Done. VB.NET, a SQL Connection, the System.ServiceModel.Syndication namespace, and no LINQ. :)

更新:上得了11-11-2010死灵法师徽章。好极了!谢谢! :)

Update: Got the Necromancer badge on 11-11-2010. Yay! Thanks! :)

这篇关于创建ASP.NET联合订阅与SqlConnection和VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆