0

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; }
}
AM0
  • 165
  • 2
  • 4
  • 20

2 Answers2

0

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();                            
    }
 }
AM0
  • 165
  • 2
  • 4
  • 20
0

If you want to do it the initial way you tried, using DBSet etc you can do that. Your issue is that you called the class sysmail_mailitem when the table name is sysmail_mailitems (with an s). So you would have to annotate the actual table name above the class like this:

[Table("sysmail_mailitems")]
public class sysmail_mailitem
{
Brett T
  • 25
  • 6