browse by category or date

SQL SMO (SQL Management Object) is a collection of assemblies that shipped together with SQL 2005. This collection of assemblies is all that you need to control your SQL Server. Be it SQL 2005, or SQL 2000, you can control/manipulate it programmatically using SQL SMO.

Since SQL SMO gave you the ability to treat the database elements such as Table, Database, Stored Procedure, and Trigger as an object, interacting with them should be relatively easy and practical. Additionally, you might want to check out this list of things that make SQL SMO exciting.

OK, lets get our hand dirty with the project. The first step would be importing the references into your project. If you use Visual Studio 2005, look for Microsoft.SqlServer.Smo in the list of .NET assembly when you open ‘Add References’ window.
If you already have MS SQL 2005 installed, you can find the DLLs in the SDKAssemblies folder of your MS SQL 2005 installation folder.
If you don’t have MS SQL 2005 installed in your computer, you might want to download the Express edition HERE.

Next, you could use the following code as reference.

using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;
using System;
using System.Configuration;
using System.Data.SqlClient;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            //Assuming that you include a Configuration file to your 
            //project and set the key 'DataSource'
            //as the connection string
            SqlConnection conn = new SqlConnection(
                      ConfigurationManager.AppSettings["DataSource"]
            );
           //Instantiate the connection to the server
	   ServerConnection dbConn = new ServerConnection(conn);
	   //Instantiate the Database Server Object
	  Server dbServer = new Server(dbConn);			

          //Traverse the Database Objects
          foreach (Database db in dbServer.Databases)
          {
              if (db.IsAccessible)
              {
                  //traverse the Stored Procedure Objects
                  foreach (StoredProcedure sp in db.StoredProcedures)
                  {
                      foreach (string str in sp.Script())
                          Console.WriteLine(str);
                  }
                  //Traverse the Table Objects
                  foreach (Table tb in db.Tables)
                  {
                      foreach (string str in tb.Script())
                          Console.WriteLine(str);
                  }
                  //Traverse the View Objects
                  foreach (View vw in db.Views)
                  {
                      foreach (string str in vw.Script())
                          Console.WriteLine(str);
                  }
              }
          }
      }
  }
}

Make sure in your app.config you have the DataSource key. You can follow this template for the configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>    
  <appSettings>
    <add key="DataSource" value="data source=db_hostname;initial catalog=db_name;UID=user_id;PWD=password"></add>
  </appSettings>
</configuration>

Have fun!

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant:

Before you imagining any physical disease that affect your body, I am actually talking about a mental-disease disease that will make your blog posting frequency reduced, then reduced more, and finally became non-existent. Of course you can argue that breaking both of your arms will make blogging out of the equation. But in case you did broke both your arms, I just hope for your quick recovery. If I may suggest, your experience during your recovery should become a valuable experience that can be shared with your future blog readers.

The disease that I want to talk is the disease of fear. Fearing that everybody already know what you are going to write. Occasionally, I did experience the same thing. This fear did prevent me writing what I have in mind.

There was a time when I was in the mood of writing about something. Then I researched about the subject through Google. The sheer amounts of article written about the subject immediately discouraged me. “Why should I waste my time. There are so many similar articles in the Net. I fear that my article will become one of those junks found in the Internet”.

Until eZineArticles send me this article. It really helped me to battle the fear and start writing.

Now every time I have this kind of fear, I just tell to myself that as long as I put my best effort, well researched it, and being personal, whatever I write will have a unique touch.

So if you occasionally infected by this disease, read the the article above. Hopefully it will help you like it helped me.

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant:

Of course. I am talking about those pills offered by Morpheus to Neo in ‘The Matrix‘ movie. Isn’t a huge coincidence that Internet Explorer is ‘Blue’ and Firefox is ‘Red’?

Now, before you telling me ‘Dude, what the heck are you talking about?’. Imagine that we as Web Developers/Programmers become Neo. Then imagine that Natural laws such as gravity, electricity and magnetism are the W3C definition for HTML, CSS and XHTML.

OK, back to the movie. If Neo chooses the Red Pill, he will wake up from his ‘artificial life’ and life in the real world. He will suffer, but he will know the real world. But if Neo chooses the Blue Pill, he will carry on in his ‘artificial life’. He might never meet the hardship of the real world.

So the premise here are ‘Red Pill’ will bring you to real life. To get something, you must follow the natural laws (e.g. W3C’s HTML Definition). But ‘Blue Pill’ will make your life easier as you don’t need to always follow the natural laws (e.g. W3C’s HTML definition) yet you will still get the same thing.

For example, to get a tooltip for your images, Internet Explorer will only need the ALT attribute in your image. But Firefox will not display the tooltip if you use ALT attribute. Because according to the natural laws (i.e. W3C’s HTML Definition), ALT is to be displayed when the Image is not available. To have the tooltip, W3C’s rules stated that you must use the TITLE attribute in your images.

So throw away your ‘Blue Pill’ and take the ‘Red Pill’. As I said, you might suffer a little bit, but you will see the real world. =)

PS: This post was inspired by my inability to see the tooltips in my WP-Translate images. Apparently I’ve been consuming the Blue Pill without realizing it. >.< Reference:
Firefox, ALT Tags, and Tooltips

About Hardono

Howdy! I'm Hardono. I am working as a Software Developer. I am working mostly in Windows, dealing with .NET, conversing in C#. But I know a bit of Linux, mainly because I need to keep this blog operational. I've been working in Logistics/Transport industry for more than 11 years.

Possibly relevant: