About Me

My photo
Fort Worth, TX, United States

Friday, July 6, 2007

Export Tables From MS Access to SQL Server

I had a couple tables in an old MS Access 2003 database that I wanted to convert to SQL Server 2005, so I tried to use the Upsizing Wizard for the first time ever. I don't know why exactly, but I could never get it to work. Kept giving me some SQL error that didn't make sense. I wasn't looking forward to recreating the tables from scratch in SQL Server, so I dug around a little more and discovered that you can right-click on a table and Export to any ODBC database. Score!

If you want to do this yourself, you'll first need to create an ODBC Data Source on your machine which connects to your SQL Server. (If you don't know how to do this, just do a web search.)

Once you have an ODBC Data Source, open your Access database, right-click, and choose Export:


Choose ODBC as the data source:



Type in the name of the new table on SQL Server (it will default to being created under dbo):



Choose the ODBC data source you want to connect to (this could be Oracle, SQL Server 2000, IBM DB2, or any other database):



Depending on your ODBC configuration, it may ask you to login:



Once you click next, it won't actually tell you anything (some feedback would have been nice), but if you go out to your SQL Server database, you should see the new table. And the nice thing is, it preserves all your data types and PKs.

Replace First Occurrence of a String in C#

I had a need to replace only the first occurrence of a substring within a string, and C# doesn't seem to have a way of doing this with the out-of-the-box String methods. So, I rolled my own:

public static string ReplaceFirstOccurrence(string original, string oldValue, string newValue)
{
    if (String.IsNullOrEmpty(original))
        return String.Empty;
    if (String.IsNullOrEmpty(oldValue))
        return original;
    if (String.IsNullOrEmpty(newValue))
        newValue = String.Empty;
    int loc = original.IndexOf(oldValue);
    return original.Remove(loc, oldValue.Length).Insert(loc, newValue);
}

If you're interested, here is the test case for this method:
[TestClass]
    public class ReplaceFirstOccurrenceTest
    {
        [TestMethod]
        public void TestReplaceFirstOccurrence()
        {
            Assert.AreEqual("", StringUtil.ReplaceFirstOccurrence(null, null, null));
            Assert.AreEqual("", StringUtil.ReplaceFirstOccurrence("", "", ""));
            Assert.AreEqual("Th is what is right.",
                StringUtil.ReplaceFirstOccurrence("This is what is right.", "is", null));
            Assert.AreEqual("Th is what is right.",
                StringUtil.ReplaceFirstOccurrence("This is what is right.", "is", ""));
            Assert.AreEqual("This is what is right.",
                StringUtil.ReplaceFirstOccurrence("This is what is right.", null, ""));
            Assert.AreEqual("This is what is right.",
                StringUtil.ReplaceFirstOccurrence("This is what is right.", "", "stuff"));
        }
    }

Wednesday, July 4, 2007

The following module was built either with optimizations enabled or without debug information

I was getting this annoying message every so often when I would build my solution in Visual Studio 2005.

I'm not really sure what it changed (or why I was getting the message), but this fixed it for me:

  • On the VS 2005 menu, go to Tools => Options => Debugging => General.
  • Uncheck: 'Enable Just My Code (Managed Only)'
If anyone knows why you get this message in the first place, I'd be interested in knowing.

Blog Archive