2015-07-28 42 views
1

這是我的控制檯應用程序。我可以壓縮它,並把它上傳爲webjob,
但我需要閱讀從我的蔚藍的分貝是與我的.NET網站上發佈如何在azure webjob應用程序中讀取azure sql db?

{ static void Main(string[] args) 


    { Console.WriteLine("[email protected]"); 
     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); 
    } 
} 

是否有可能在webjob讀蔚藍DB數據?如果是這樣,怎麼樣?

回答

2

是的,你可以。一種方法是連接字符串添加到您的App.config文件

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

而在代碼中使用它:

... 
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 
      } 
     } 
    } 
} 
+0

那麼你如何調用表的實例在DB像asp.net我們會說一些像productsdb obj = new productdb()然後obj.datafield –

+0

如果你想使用實體框架喲必須添加對它的引用。如果您想使用應用程序中的現有模型,則還需要添加對此項目的引用。 – py3r3str

相關問題