我通过 Visual Studio 运行了 azure 函数。该函数从也在 Azure (SQL Server) 上托管的 SQL 数据库返回一些数据,在本地主机上一切正常,但是当我将其发布到 azure 时,它返回 500 错误。
在 Azure SQL 上,我检查了来自其他 Azure 服务和我的 IP 地址的访问。
本地.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "",
"AzureWebJobsDashboard": ""
},
"ConnectionStrings": {
"MySqlConnectionString": {
"ConnectionString":
"Server=tcp:webappgtoproject.database.windows.net,1433;Initial Catalog=database;Persist Security Info=False;User ID=xxx;Password=yyy;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
"providerName": "System.Data.SqlClient"
}
}
}
代码
string str = ConfigurationManager.ConnectionStrings["MySqlConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(str))
{
conn.Open();
// Check PC
string text2 = "command text";
using (SqlCommand cmd = new SqlCommand(text2, conn))
{
}
return true;
}
如果没有 SQL 连接,它可以在 Azure 上正常工作。
最佳答案
local.settings.json
文件仅在本地运行时使用。您需要在应用程序设置中定义连接字符串(单击您的功能应用程序并选择应用程序设置
链接)。
关于c# - 通过 Azure 函数的 SQL 连接仅适用于本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47209038/