[英]How to read azure sql db in azure webjob application?
這是我的控制台應用程序。 我可以將其壓縮並上傳為網絡作業,
但是我需要從.net站點發布的Azure數據庫中讀取數據
{ 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? 如果是這樣,怎么辦?
是的你可以。 一種方法是將連接字符串添加到您的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
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.