如何在azure webjob应用程序中读取azure sql数据库

本文关键字:azure 读取 sql 数据库 webjob 应用程序 | 更新日期: 2023-09-27 18:05:10

这是我的控制台应用程序。我可以将其压缩并作为webjob上传,
但我需要从我的azure数据库中读取与我的。net网站发布的数据

{  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);
    }
}

是否可以在webjob中读取azure db ?如果有,怎么做?

如何在azure webjob应用程序中读取azure sql数据库

可以。一种方法是将连接字符串添加到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
             }
         }
    }
}