1

This is my console app. I can zip it and upload it as a webjob,
but I need to read data from my azure db that is published with my .net site

{  static void Main(string[] args)


    { Console.WriteLine("shanzzzh@gmail.com");
      MailAddress to = new MailAddress(Console.ReadLine());
 Console.WriteLine("Mail From");
        MailAddress from = new MailAddress(Console.ReadLine());
       MailMessage mail = new MailMessage(from,to );

      Console.WriteLine("Subject");
        mail.Subject = Console.ReadLine()

       Console.WriteLine("Your Message");
        mail.Body = Console.ReadLine()
       SmtpClient smtp = new SmtpClient();
        smtp.Host = "pod51014.outlook.com";
        smtp.Port = 587 
        smtp.Credentials = new NetworkCredential( "*********", "******");
        smtp.EnableSsl = true;

        Console.WriteLine("Sending email...");
        smtp.Send(mail);
    }
}

Is it possible to read azure db in a webjob? If so, how?

Silvia Doomra
  • 947
  • 8
  • 16

1 Answers1

2

Yes you can. One way is to add connection string to your App.config file

<configuration>
...
    <connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=mssql5.webio.pl,2401;Database=ypour_connection_string" providerName="System.Data.SqlClient"/>
    </connectionStrings>
...

And use it in code:

...
String connString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (var conn = new SqlConnection(connString))
{
    conn.Open();
    using (SqlCommand command = new SqlCommand(@"your_sql_command"))
    {
         using (SqlDataReader reader = command.ExecuteReader())
         {
             while (reader.Read())
             {
                  //do stuff
             }
         }
    }
}
Community
  • 1
  • 1
py3r3str
  • 1,879
  • 18
  • 23
  • so how do you call the instance of a table in the db like with asp.net we would say something like productsdb obj = new productdb () and then obj.datafield – Preson Reddy Jul 28 '15 at 12:16
  • If you want to use Entity Framework yo have to add references to it. If you want to use existing models from your application you also need to add reference to this project. – py3r3str Jul 28 '15 at 12:24