Ask a questionAsk a question
 

AnswerCannot Find Some SqlServer.Management objects

  • Saturday, September 26, 2009 12:58 AMDudeSmiles Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I have some C# code in .Net 3.5 to find the disk drive of where our product's SQL Server database files are stored (the code also does a few other database management-related tasks). I've been using something like this:

    using Microsoft.SqlServer.Management.Common;
    using Microsoft.SqlServer.Management.Smo;
    using System.Data.SqlClient;
    .
    .
    .
    // Connect to SQL Server to gain access to the database.
    SqlConnection sqlConnection = new SqlConnection("MyConnectionString");
    ServerConnection serverConnection = new ServerConnection(sqlConnection);
    Server server = new Server(serverConnection);
    
    foreach (FileGroup fg in server.Databases["MyDatabase"].FileGroups)
        foreach (DataFile f in fg.Files)
        {
            // Get drive of where data and log files are stored
        }
    
    // I also access other objects, such as...
    double dataSpaceUsed = server.Databases["CEMLink"].DataSpaceUsage;
    double indexSpaceUsed = server.Databases["CEMLink"].IndexSpaceUsage;
    

    In 4.0, however, this code will not compile because it cannot find the Microsoft.SqlServer.Management.Common.Server object. If I set the project's Target Framework to 3.5 this code works great, but as soon as I change the Target Framework to 4.0 I loose the Server object. I have references to Microsoft.SqlServer.ConnectionInfo, Microsoft.SqlServer.Management.Sdk.Sfc, Microsoft.SqlServer.Smo and Microsoft.SqlServer.SqlEnum.

    Anyone know where I can find the equivalent of this object in .Net 4.0? Or have an idea of how I can find out the disk drive of where our database files are stored? Thanks!

    Side-Note: The ADO.NET Data Services is probably not the best place to post this issue (my apologies), but I wasn't sure where was best. So thanks for everyone's consideration.

    • Edited byDudeSmiles Saturday, September 26, 2009 1:00 AMtouching up some <BR> tags in the code
    • Edited byDudeSmiles Saturday, September 26, 2009 1:01 AMtrying to touch up spacing for read-ability
    •  

Answers

  • Tuesday, September 29, 2009 9:40 PMDudeSmiles Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I have this working now, and I can consistently get it to work and cause it to fail.

    To get it to fail (i.e., reproduce the problem above):
    I go into my original project (which has a Targeted Framework version of 3.5) and change the Framework to 4.0. Visual Studio then does some "behind the scenes" work to port the project to 4.0. When it's done, the project will no longer build. References to the required assemblies do appear in the References list (Microsoft.SqlServer.ConnectionInfo, Microsoft.SqlServer.Management.Sdk.Sfc, Microsoft.SqlServer.Smo and Microsoft.SqlServer.SqlEnum).

    To get it to work after converting it from 3.5:
    Remove the references to Microsoft.SqlServer.ConnectionInfo, Microsoft.SqlServer.Management.Sdk.Sfc, Microsoft.SqlServer.Smo and Microsoft.SqlServer.SqlEnum, and re-add them through the Browse tab in the Add Reference... dialog.

    If I start from scratch with a new project targeting the 4.0 Framework, I don't have any problems. But the references still have to be added using the Browse tab in the Add Reference... dialog (as opposed to the .NET tab).

All Replies

  • Tuesday, September 29, 2009 9:40 PMDudeSmiles Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I have this working now, and I can consistently get it to work and cause it to fail.

    To get it to fail (i.e., reproduce the problem above):
    I go into my original project (which has a Targeted Framework version of 3.5) and change the Framework to 4.0. Visual Studio then does some "behind the scenes" work to port the project to 4.0. When it's done, the project will no longer build. References to the required assemblies do appear in the References list (Microsoft.SqlServer.ConnectionInfo, Microsoft.SqlServer.Management.Sdk.Sfc, Microsoft.SqlServer.Smo and Microsoft.SqlServer.SqlEnum).

    To get it to work after converting it from 3.5:
    Remove the references to Microsoft.SqlServer.ConnectionInfo, Microsoft.SqlServer.Management.Sdk.Sfc, Microsoft.SqlServer.Smo and Microsoft.SqlServer.SqlEnum, and re-add them through the Browse tab in the Add Reference... dialog.

    If I start from scratch with a new project targeting the 4.0 Framework, I don't have any problems. But the references still have to be added using the Browse tab in the Add Reference... dialog (as opposed to the .NET tab).