gpt4 book ai didi

c# - 如何在azure webjob应用程序中读取azure sql db?

转载 作者:行者123 更新时间:2023-12-03 01:53:07 25 4
gpt4 key购买 nike

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

{  static void Main(string[] args)


{ Console.WriteLine("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f98a91989783838391b99e94989095d79a9694" rel="noreferrer noopener nofollow">[email protected]</a>");
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);
}
}

是否可以在网络作业中读取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
}
}
}
}

关于c# - 如何在azure webjob应用程序中读取azure sql db?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31672947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com