我正在设置 CRM 360 数据导出服务 (DES) 需要连接的 Azure 数据库。我可以使用 SSMS 手动执行此操作并使用我的 Azure 帐户登录。
然后将 DES 服务帐户设置为外部提供商。
从外部提供商创建用户 DynamicsDataExportService
一切都好,但是我希望自动化该过程,因此我需要使用 C# 登录服务并以这种方式连接和运行代码。
我使用以下代码运行 core 2.2 和 System.Data.SqlClient 4.7:
var cs = "data source=tcp:dvzxfsdg-sql.database.windows.net,1433;initial catalog=dfsdfs-sqldb; Authentication=Active Directory Integrated";
using (var connection = new SqlConnection(cs))
{
connection.Open();
令人烦恼的是它给出了错误
"System.ArgumentException: 'Keyword not supported: 'authentication'.'".
我很确定我犯的错误是相当基本的,但我似乎无法追根究底。因此,我们非常感谢您的任何指点。
最佳答案
您可能想尝试使用SQLConnectionStringBuilder
来正确设置连接字符串的格式。这有助于避免使用明文连接字符串时可能遇到的拼写错误和问题。之后,您实际上可以直接在构建器上设置 AuthenticationMethod。
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder ()
{
DataSource = "ServerName",
InitialCatalog = "DatabaseName",
etc...
}
builder.Authentication = SqlAuthenticationMethod.ActiveDirectoryInteractive;
SqlConnection sqlConnection = new SqlConnection(builder.ConnectionString);
如果 SQL 数据库是 Azure SQL 数据库,则您可以查看资源的概述页面并找到“连接字符串”边栏选项卡。这可能有助于解决您所提供的连接字符串所遇到的问题。
查看this answer有关构建连接字符串的更多信息
关于c# - 使用 Active Directory 密码从 C# 连接到 Sql Azure Db 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58881087/