【问题标题】:How to use SMO ServerConnection and Database with a contained user in Azure SQL Database如何将 SMO ServerConnection 和数据库与 Azure SQL 数据库中包含的用户一起使用
【发布时间】:2019-11-03 06:52:00
【问题描述】:

有没有办法为无权访问master的用户使用SMO?

我正在通过 SMO 编写 Azure SQL 数据库的 DLL。我使用具有 db_owner 角色的包含用户来访问 db 和 DDL,但它会引发异常。 SMO 在我创建 ServerConnection 后立即尝试默认为 master,其中包含的用户没有权限。

我在 .NET Core 3.0 中使用了在 nuget 和代码上发布的最新 SMO 版本。

--exec in master db
CREATE LOGIN testuser WITH PASSWORD='...';

--exec in user db: testsqldb
CREATE USER testuser FROM LOGIN testuser
ALTER ROLE db_owner ADD MEMBER testuser

连接字符串有InitialCatalog = testsqldb

var connectionStringBuilder = new SqlConnectionStringBuilder(sourceConnectionString);
var serverConnection = CreateServerConnection(_connectionString);

//this throws exception
var server = new Server(serverConnection);

异常详情

{"The server principal \"testuser\" is not able to access the database \"master\" under the current security context.\r\nCannot open user default database. Login failed.\r\nLogin failed for user 'testuser'."}

我希望 ServerConnection 遵守 ConnectionString 中指定的 InitialCatalog,而不是默认为 'master'

【问题讨论】:

    标签: azure-sql-database smo


    【解决方案1】:

    我已经针对 SQL Azure 测试了以下代码:

    ServerConnection conn = new ServerConnection(instanceUrl, username, password);
    conn.DatabaseName = database;
    _sqlServer = new Server(conn);
    

    【讨论】:

      【解决方案2】:

      请参考这个SMO官方教程:Connecting to an Instance of SQL Server by Using SQL Server Authentication in Visual Basic

      当您使用 SQL Server 身份验证连接到 SQL Server 实例时,您必须指定身份验证类型。此示例演示了声明 ServerConnection 对象变量的替代方法,该方法可以重用连接信息。

      示例是演示如何连接到远程的 Visual Basic .NET 代码,vPassword 包含登录名和密码。

      ' compile with:   
      ' /r:Microsoft.SqlServer.Smo.dll  
      ' /r:Microsoft.SqlServer.ConnectionInfo.dll  
      ' /r:Microsoft.SqlServer.Management.Sdk.Sfc.dll   
      
      Imports Microsoft.SqlServer.Management.Smo  
      Imports Microsoft.SqlServer.Management.Common  
      
      Public Class A  
         Public Shared Sub Main()  
            Dim sqlServerLogin As [String] = "user_id"  
            Dim password As [String] = "pwd"  
            Dim instanceName As [String] = "instance_name"  
            Dim remoteSvrName As [String] = "remote_server_name"  
      
            ' Connecting to an instance of SQL Server using SQL Server Authentication  
            Dim srv1 As New Server()   ' connects to default instance  
            srv1.ConnectionContext.LoginSecure = False   ' set to true for Windows Authentication  
            srv1.ConnectionContext.Login = sqlServerLogin  
            srv1.ConnectionContext.Password = password  
            Console.WriteLine(srv1.Information.Version)   ' connection is established  
      
            ' Connecting to a named instance of SQL Server with SQL Server Authentication using ServerConnection  
            Dim srvConn As New ServerConnection()  
            srvConn.ServerInstance = ".\" & instanceName   ' connects to named instance  
            srvConn.LoginSecure = False   ' set to true for Windows Authentication  
            srvConn.Login = sqlServerLogin  
            srvConn.Password = password  
            Dim srv2 As New Server(srvConn)  
            Console.WriteLine(srv2.Information.Version)   ' connection is established  
      
            ' For remote connection, remote server name / ServerInstance needs to be specified  
            Dim srvConn2 As New ServerConnection(remoteSvrName)  
            srvConn2.LoginSecure = False  
            srvConn2.Login = sqlServerLogin  
            srvConn2.Password = password  
            Dim srv3 As New Server(srvConn2)  
            Console.WriteLine(srv3.Information.Version)   ' connection is established  
         End Sub  
      End Class
      

      您可以使用新登录名/用户testuser 连接到testsqldb,方法是更改​​ SMO。

      希望这会有所帮助。

      【讨论】:

      • 此问题似乎与连接到 Az SQL 数据库时的 SMO 行为有关。类似的情况,当我尝试捕获源数据库时,它会自动切换到主数据库。服务器 = 新服务器(连接); //服务器在这行之后自动切换到master database = server.Databases[connectionStringBuilder.InitialCatalog]; stackoverflow.com/questions/5798187/…
      • 谢谢,@rdagumampan。你通过参考这个博客解决了吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 2010-09-21
      • 2021-02-05
      • 1970-01-01
      相关资源
      最近更新 更多