【问题标题】:Create ASP.NET Syndication Feeds with SqlConnection and VB.NET使用 SqlConnection 和 VB.NET 创建 ASP.NET 联合源
【发布时间】:2010-10-27 05:27:53
【问题描述】:

这似乎是 Scott Mitchell 撰写的一篇很棒的文章,用于在 ASP.NET 3.5 中创建联合提要。对我来说,问题是它使用了 C# 和 Linq,我目前还没有那么敏锐。

http://dotnetslackers.com/articles/aspnet/How-to-create-a-syndication-feed-for-your-website.aspx

有谁知道System.ServiceModel.Syndication 命名空间的示例可以使用VB.NETSQLConnection 对象生成像本文这样的联合提要吗?

我环顾四周,每个示例似乎都是用 C# 和 Linq 生成的(这可能证明我需要尽快而不是稍后学习它们)。

【问题讨论】:

    标签: asp.net sql vb.net rss syndication


    【解决方案1】:

    您现在可能已经想通了,但这里有一个实现完整性和一些 VB 的爱(以及对 Necromancer badge 的尝试。:)

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

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

    您可能想考虑改用 HttpHandler,但这也可以正常工作。

    背后的代码:

    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脚本:

    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。 :)

    更新:在 2010 年 11 月 11 日获得死灵法师徽章。耶!谢谢! :)

    【讨论】:

      【解决方案2】:

      我不知道在 VB.Net 中使用这些对象的示例,但是有几种方法可以将 c# 代码转换为 VB.Net。您可以使用 SharpDevelop 之类的 IDE,也可以使用多种免费在线转换器中的任何一种。

      我最喜欢的技术是将源下载或剪切并粘贴到 Visual Studio 中,然后用 C# 编译项目。然后使用Reflector将IL反汇编成VB.Net。通过这样做,您可以将 C# 与 VB.Net 进行比较,如果您愿意,它可能会帮助您看到相似之处并更快地学习 C#。

      【讨论】:

      • 实际上,我确实是通过找到翻译来做到这一点的。这是 Linq 代码,我现在翻译得不好。
      • 您是否尝试过 Microsoft 的 Visual LINQ Query Builder 插件? code.msdn.microsoft.com/vlinq 这是一个不错的工具,类似于旧的 Access Query Builder。
      • 好吧,坦率地说,我不想在这个项目中使用 LINQ。至少,我现在正在尝试坚持使用 SQL 数据层。
      猜你喜欢
      • 2018-07-05
      • 2020-12-11
      • 2021-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多