对于无法访问 0.9572 0.9572 0.9572 0.9572 0.9572 0.9572 0.9572 0.9572 0.9572 0.9572
0.4854 0.4854 0.4854 0.4854 0.4854 0.4854 0.4854 0.4854 0.4854 0.4854
0.8003 0.8003 0.8003 0.8003 0.8003 0.8003 0.8003 0.8003 0.8003 0.8003
0.1419 0.1419 0.1419 0.1419 0.1419 0.1419 0.1419 0.1419 0.1419 0.1419
的用户,是否可以使用SMO?
我正在通过SMO编写Azure SQL数据库的DLL脚本。我使用了一个具有db_owner角色的包含用户来访问db和DDL,但它引发异常。我创建ServerConnection时,SMO会尝试默认设置为master
,对此包含的用户没有权限。
我使用了.net Core 3.0中在nuget和代码上发布的最新SMO版本。
master
连接字符串具有--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);
我希望ServerConnection尊重ConnectionString中指定的InitialCatalog,而不默认为“ master”
答案 0 :(得分:1)
我已经针对SQL Azure测试了以下代码:
ServerConnection conn = new ServerConnection(instanceUrl, username, password);
conn.DatabaseName = database;
_sqlServer = new Server(conn);
答案 1 :(得分:0)
请参考此官方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
您可以通过更改SMO与新的登录名/用户testsqldb
连接到testuser
。
希望这会有所帮助。