Recently I’ve moved to a new content management system (dasBlog).My old way of doing thing was to write my own blogging software and use that. Thesad fact is that I just didn’t have the time of patience to work out the bugs anddasBlog offered a lot more features. The only problem is that dasBlog stores entriesas XML text files and my old system stored them in a SQL Server database. Thus I neededa way to move hundred of entries out of SQL Server and into dasBlog formatted XML.My first thought was some type of fancy FOR XML query that would spit out the XMLand then I’d have to somehow parse that ginormous file into day-by-day XML files.Yeah, I went with that idea for about 5 minutes. All my searches turned up nothingspectacular (unless of course I was converting Radio UserLand to dasBlog) until Ifound Blobservations.So, thanks to Rick’scode I was able to import all my old posts from start to finish in under fortyminutes last night. I used pretty much the same logic, but wrote my script to accessmy database instead of a blogger exported XML file. Here’s the quick and dirty codeif you need to export blog entries from a database into dasBlog:

    Public Function ExportToDasBlog() As Integer
        ’setup local constants
        ‘if you’re copy and pasting this code, 
        ‘ all you need to change are the constant values below
        Const exportDirectory As String = “C:\dasBlogContent”
        Const commandText As String = “SELECT * FROM MyTable with (nolock)”
        Const connectionString As String = “Data Source=www.myserver.com;” & _
                                            “Initial Catalog=myDatabase;” & _
                                            “User ID=myName;” & _
                                            “Password=myPassword”
        Const author As String = “Jeffrey Kelso”
        Const contentFieldName As String = “Content”
        Const dateCreatedFieldName As String = “DateAdded”
        Const dateModifiedFieldName As String = “DateEdited”
        Const titleFieldName As String = “Subject”

        ‘create the service that will save the entries
        Dim dasBlogDataService As _
          newtelligence.DasBlog.Runtime.IBlogDataService = _
          newtelligence.DasBlog.Runtime.BlogDataServiceFactory.GetService(exportDirectory, Nothing)
        ‘counter for records processed
        Dim i As Integer = 0
        ‘object to build our new post
        Dim entry As newtelligence.DasBlog.Runtime.Entry

        ‘build our command and open the connection
        Dim cmd As New SqlClient.SqlCommand(commandText)
        cmd.Connection = New SqlClient.SqlConnection(connectionString)
        cmd.Connection.Open()

        ‘get a datareader of records to process
        Dim dr As SqlClient.SqlDataReader = _ 
          cmd.ExecuteReader(CommandBehavior.CloseConnection)

        ‘loop through the records and convert them to a dasBlog format
        While dr.Read

            entry = New newtelligence.DasBlog.Runtime.Entry

            entry.Author = author
            entry.Content = dr(contentFieldName).ToString.Trim
            entry.CreatedLocalTime = CDate(dr(dateCreatedFieldName))
            entry.CreatedUtc = CDate(dr(dateCreatedFieldName)).ToUniversalTime
            ‘entry.Description = I don’t have a description, maybe you do
            entry.EntryId = Guid.NewGuid.ToString
            entry.IsPublic = True
            If Not dr(dateModifiedFieldName) Is System.DBNull.Value Then _
                entry.ModifiedLocalTime = CDate(dr(dateModifiedFieldName))
            If Not dr(dateModifiedFieldName) Is System.DBNull.Value Then _
                entry.ModifiedUtc = CDate(dr(dateModifiedFieldName)).ToUniversalTime
            entry.Title = dr(titleFieldName).ToString.Trim

            ‘use the dasBlogDataService to save the new object 
            ‘ to our export directory
            dasBlogDataService.SaveEntry(entry)

            ‘increment our count
            i += 1

        End While

        ‘close the datareader, connection, and return
        ‘the count of records exported
        dr.Close()
        Return i

    End Function



June 1st, 2005  
Leave a comment
Name:


Email: (will not be published)


Website:


Comments:



 

My Photos
www.flickr.com
This is a Flickr badge showing photos in a set called Weblog photos. Make your own badge here.