Search code examples
c#sql-serverentity-frameworksystem-tables

Read Data from 'MSDB' System Table


In my application I want to retrieve blind_copy_recipients from system table 'sysmail_mailitems' from database 'MSDB' in SQL Server 2012. I am using Entity Framework in a C# web application to query databases. I created a class for sysmail_mailitems and method to read data from it. But it actually created a new table outside of system tables with this name. My goal is not to create a table but just to read from existing table. Can anyone please guide me on how could I do it?

Code:

public class sysmail_mailitem
{
    [Key]
    public Int32 mailitem_id { get; set; }

    public Int32 profile_id { get; set; }
    public String recipients { get; set; }
    public String copy_recipients { get; set; }
    public String blind_copy_recipients { get; set; }
    public String subject { get; set; }
    public String from_address { get; set; }
    public String reply_to { get; set; }
    public String body { get; set; }
    public String body_format { get; set; }
    public String importance { get; set; }
    public String sensitivity { get; set; }
    public String file_attachments { get; set; }
    public String attachment_encoding { get; set; }
    public String query { get; set; }
    public String execute_query_database { get; set; }
    public Boolean? attach_query_result_as_file { get; set; }
    public Boolean? query_result_header { get; set; }
    public Int32? query_result_width { get; set; }
    public String query_result_separator { get; set; }
    public Boolean? exclude_query_output { get; set; }
    public Boolean? append_query_error { get; set; }
    public DateTime send_request_date { get; set; }
    public String send_request_user { get; set; }
    public Int32? sent_account_id { get; set; }
    public Byte? sent_status { get; set; }
    public DateTime? sent_date { get; set; }
    public DateTime last_mod_date { get; set; }
    public String last_mod_user { get; set; }
}

public String GetMailRecipients(Int32 mailItemId)
{
     using(MSDBContext _db = new MSDBContext())
     {
         var query = (from mailItems in _db.MailItems
                         where mailItems.mailitem_id == mailItemId
                         select mailItems).FirstOrDefault();
         try
         {
                return query.blind_copy_recipients;
         }
         catch (NullReferenceException) { }
         return "N/A";
     }
 }


public class MSDBContext : DbContext
{
    public MSDBContext() : base("msdb") { }

    public DbSet<sysmail_mailitem> MailItems { get; set; }
}

Solution

  • In the end I came up with executing raw sql command using ExecuteStoreQuery to retrieve data from MSDB.

    Code:

    public String GetMailRecipients(Int32 mailItemId)
    {
       using(context _db = new context())
       {                                        
            var obj = ((IObjectContextAdapter)_db).ObjectContext;
            return obj.ExecuteStoreQuery<String>("SELECT blind_copy_recipients FROM msdb.dbo.sysmail_mailitems WHERE mailitem_id = {0}", mailItemId).FirstOrDefault();                            
        }
     }