2015-07-28 61 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

相关问题