Migrating Content from Random Blogs to DasBlog
This guy Ernie emailed me saying that he had some random, possibly home-grown, blog with content in Access that he wanted to migrate to his new DasBlog. I don't ordinarily do this (and don't plan to ever again) but it's so easy to move content into DasBlog that I decided to take this opportunity to write an example I could point other folks to.
Note: this is just an example that converted Ernie's blog. It's not something that will work unchanged for you, so don't ask. :) Ernie had a "news" table and a "comments" table. Blog posts were in news and used "blogid" as a unique id.
Additional note: Often you can use the same unique ids in DasBlog. You don't need you use a GUID for the DasBlog EntryId. In this case, Ernie's BlogID was just an int and it was unique enough. He could implement redirects if someone visited /myoldblog.asp?id=4 to /mynewdasblog/permalink.aspx?guid=4 and all his old content would get reindexed by Google and none of the old links would be invalid.
Just make a Console application in C# or VB.NET and add a reference to newtelligence.DasBlog.Runtime. I used a DataReader and just hard-coded the indexes of each column. It's not like this will ever be run again. It takes a little while to run because the DasBlog engine expects to run in the context of ASP.NET so it isn't able to use the ASP.NET Cache. The performance degrades O^n when run locally as your content folder grows. If that becomes a problem, run your converter in the context of IIS/ASP.NET and you'll get the high speed access.
In this example, the DasBlog dayentry.xml and dayfeedback.xml files will be left in the directory that the application was run in. I also converted newlines to <br> and when a blog post didn't include a title, I used the first 20 characters of the post as the title.
This took about 15 mins at lunch, so you really can transfer all your existing blog's data to DasBlog quickly, usually using just the Entry class and the Comment class. Note how they relate to each other using the EntryId as the key.
static void Main(string[] args)
{
IBlogDataService dataService =
BlogDataServiceFactory.GetService(AppDomain.CurrentDomain.BaseDirectory,null);
string connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;User ID=Admin;
Data Source=ernie.mdb;Mode=Share Deny None";
using(OleDbConnection conn = new OleDbConnection(connStr))
{
conn.Open();
using(OleDbCommand newsCmd = new OleDbCommand("select * from News",conn))
{
using (OleDbDataReader reader = newsCmd.ExecuteReader())
{
while(reader.Read())
{
int blogId = reader.GetInt32(0);
DateTime date = reader.GetDateTime(1);
DateTime time = reader.GetDateTime(3);
DateTime correctDate =
new DateTime(date.Year,date.Month,date.Day,time.Hour,time.Minute,time.Second);
string blogText = reader.GetString(2);
string blogTitle = reader.IsDBNull(4) ? String.Empty : reader.GetString(4);
Entry entry = new Entry();
entry.CreatedLocalTime = correctDate;
entry.ModifiedLocalTime = correctDate;
entry.Title =
(blogTitle.Length > 0 ? blogTitle :
blogText.Substring(0,Math.Min(20,blogText.Length)));
entry.Content = blogText.Replace("\r\n","<br>");
entry.EntryId = blogId.ToString();
entry.Categories = "main;old site";
entry.Author = "Ernie";
dataService.SaveEntry(entry);
}
}
}
using(OleDbCommand newsCmd = new OleDbCommand("select * from comments",conn))
{
using (OleDbDataReader reader = newsCmd.ExecuteReader())
{
while(reader.Read())
{
int blogId = reader.GetInt32(1);
DateTime date = reader.GetDateTime(4);
string commentText = reader.GetString(2);
string commentName = reader.GetString(3);
Comment comment = new Comment();
comment.CreatedLocalTime = date;
comment.ModifiedLocalTime = date;
comment.TargetEntryId = blogId.ToString();
comment.Author = commentName;
comment.Content = commentText;
dataService.AddComment(comment);
}
}
}
}
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
Of course, those things have a way of sticking around, so python still seems like a better choice. C# is just overkill.
Saweet, thanks Scott.
Nick
Comments are closed.