gpt4 book ai didi

sql - 如何在 Azure Function 中使用 Microsoft.Data.SqlClient?

转载 作者:行者123 更新时间:2023-12-03 06:42:25 26 4
gpt4 key购买 nike

我通过门户创建了一个 Azure 函数,并希望使用 Active Directory 托管标识打开与 Sql Server 的连接。为了使该连接字符串正常工作,我必须使用 Microsoft.Data.SqlClient。但是当我尝试在 Azure 内部使用这个库时,该函数停止工作。

就像添加一样简单:

using Microsoft.Data.SqlClient

到门户内新创建的 Azure 函数的顶部。我无法使用标准 System.Data.SqlClient 命名空间,因为当我创建新的连接字符串时,它不喜欢

的连接字符串
Server=db.database.windows.net; Authentication=Active Directory Managed Identity;Encrypt=True;Database=myDb

如何在 Azure 函数内使用 Active Directory 托管身份验证方法打开 Sql 连接?

最佳答案

您需要在 function.proj 文件中添加包引用并将其上传到 Function App,然后包将添加到代码中。以下是我连接 SQL 数据库并从表中检索数据的步骤。

  1. 已经用一张表创建了sql数据库。

  2. 接下来使用运行时堆栈 .Net 和版本 3.1 创建了一个函数应用。

  3. 将 Sql 数据库连接字符串添加到函数应用配置中,如下所示。 enter image description here

  4. 创建了一个 Http 触发函数并用以下代码替换了默认代码,

#r "Newtonsoft.Json"

#r "System.Configuration"

using System;

using System.Data;

using System.Net;

using Newtonsoft.Json;

using System.Data.SqlClient;

using Microsoft.Extensions.Configuration;

using System.Configuration;

using Microsoft.AspNetCore.Mvc;

using Microsoft.Extensions.Primitives;

public static async Task<object> Run(HttpRequestMessage req, ILogger log)

{

string responseMessage;

//We retrieve the id field, which comes as a parameter to the function, by deserializing req.Content.

string jsonContent = await req.Content.ReadAsStringAsync();

dynamic data = JsonConvert.DeserializeObject(jsonContent);

//If there is no username, we return the error message.

var connectionString = Environment.GetEnvironmentVariable("SqlConnection", EnvironmentVariableTarget.Process);

//Azure SQLDB Log

var logAdded = true;

try

{

//We get the Connection String in the Function App Settings section we defined.

using(SqlConnection connection = new SqlConnection(connectionString))

{

//Opens Azure SQL DB connection.

connection.Open();

string qs = $"SELECT * FROM [dbo].[Persons] where [Personid] = 1";

SqlCommand command = new SqlCommand(qs, connection);

string queryop = "";

using (SqlDataReader reader = command.ExecuteReader())

{

queryop = sqlDatoToJson(reader);

}

responseMessage = (queryop);

connection.Close();

}

}

catch(Exception e)

{

logAdded = false;

log.LogError(e.ToString());

responseMessage = e.ToString();

// connection.Close();

}

return new OkObjectResult(responseMessage);

}

static String sqlDatoToJson(SqlDataReader dataReader)

// transform the returned data to JSON

{

var dataTable = new DataTable();

dataTable.Load(dataReader);

string JSONString = string.Empty;

JSONString = JsonConvert.SerializeObject(dataTable);

return JSONString;

}
  • 在名为 function.proj 的文件中添加了以下代码,并使用上传选项上传到函数。
  • <Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    </PropertyGroup>
    <ItemGroup>
    <PackageReference Include="Dapper" Version="2.0.78" />
    <PackageReference Include="Microsoft.Data.SqlClient" Version="4.2.1.0" />
    <PackageReference Include="Microsoft.WindowsAzure.ConfigurationManager" Version="3.2.3" />
    </ItemGroup>
    </Project>

    enter image description here6. 文件上传后,您可以在下拉列表中看到文件。 enter image description here7. 测试功能并能够从数据库获取数据, enter image description here引用link

    关于sql - 如何在 Azure Function 中使用 Microsoft.Data.SqlClient?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74406162/

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