使用 SqlConnection 和 VB.NET 创建 ASP.NET 聚合源

发布于 2024-07-22 07:19:25 字数 582 浏览 6 评论 0原文

这似乎是 Scott Mitchell 撰写的一篇关于在 ASP.NET 3.5 中创建联合提要的精彩文章。 对我来说,问题是它使用 C# 和 Linq,目前我对这两种语言不太熟悉。

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

有谁知道哪里可能存在 System.ServiceModel.Syndicate 的示例> 可以使用 VB.NETSQLConnection 对象生成像本文这样的联合提要的命名空间?

我环顾四周,发现每个示例似乎都是用 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.

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

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?

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你不是我要的菜∠ 2024-07-29 07:19:25

您现在可能已经明白了,但这里有一个为了完整性和一些 VB 热爱而实现的实现(以及 Necromancer 徽章 :)

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.Synmination 命名空间,并且没有 LINQ。 :)

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

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

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" %>

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

The code behind:

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

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

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

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

柒夜笙歌凉 2024-07-29 07:19:25

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

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

I don't know of an example using those objects in VB.Net, but there are several ways to transform c# code to VB.Net. You could use an IDE like SharpDevelop, you could use any of several free online converters.

My favorite technique would be to download or cut and paste the source into visual studio and compile the project in C#. Then use Reflector to disassemble the IL to VB.Net. By doing that you can compare the C# to VB.Net and it may help you see the similarities and pick up C# faster if that is what you would like to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文