SMO Connection to a Database and Return Server Information

Tuesday, July 22 2008 - ,

Here is a Quick sample which shows how to connect to a Database Server and collect the Base information using c# and the Microsoft SQL Server Management Objects (SMO). To use this example you will need to make sure that you add (Reference) the SQL Server Management Assemblies to your project.

Here is a list of the Assemblies you will need to reference.

  • Microsoft.SqlServer.ConnectionInfo
  • Microsoft.SqlServer.Smo
  • Microsoft.SqlServer.SmoEnum
  • Microsoft.SqlServer.SqlEnum

Here is the Code Sample...

using System;
using Microsoft.SqlServer.Management.Common;
using Microsoft.SqlServer.Management.Smo;

namespace ConnectToSQLServer
{
   
class Program
   
{
       
static void Main(string[] args)
       
{
           
Server srv = new Server("DatabaseServer\\InstaceName");
 ServerConnection srvConn = srv.ConnectionContext;

 srvConn.LoginSecure = true;
           
           
Console.WriteLine("Network Name:             {0}", srv.Information.NetName);
           
Console.WriteLine("Database Instance Name:   {0}", srv.InstanceName);
           
Console.WriteLine("Version String:           {0}", srv.Information.VersionString);

           
Console.WriteLine("Version:                  {0}", srv.Information.Version);
           
Console.WriteLine("Edition:                  {0}", srv.Information.Edition);
           
Console.WriteLine("Processors:               {0}", srv.Information.Processors);
           
Console.WriteLine("Physical Memory:          {0}", srv.Information.PhysicalMemory);
           
Console.WriteLine("Operating System Version: {0}",srv.Information.OSVersion);
           
Console.WriteLine("Product Level:            {0}", srv.Information.ProductLevel);
           
Console.WriteLine("Product:                  {0}", srv.Information.Product);

           
Console.ReadLine();
       
}
   
}
}

 

Technorati Tags: ,
kick it on DotNetKicks.com kick it on gamedevkicks.com

Similar Posts

  1. Reading a list of Servers from XML using LINQ
  2. Creating a DiggIt Module for Community Server 2007
  3. Migrating from SQL Express to SQL Server 2005 Part 1
Clicky Web Analytics