Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Scripting Remote Database using SMO in .net


Scripting Remote Database using SMO in .net

Author
Message
thulani.moyana
thulani.moyana
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 64
This following code throws an error when scripting remote database.
Connection to remote server is ok.
Login credentials supplied.

Any help will be highly appreciated.




dBase = srvr.Databases(SourceDatabase.Name)
'include the database create syntax
Dim dbso As ScriptingOptions = New ScriptingOptions()
dbso.FileName = path
dbso.AppendToFile = False

dBase.Script(dbso)




RBarryYoung
RBarryYoung
SSCrazy Eights
SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)

Group: General Forum Members
Points: 9436 Visits: 9517
We cannot see the error from here. Could you please tell us what it is?

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
thulani.moyana
thulani.moyana
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 64
Thanx once more.

The error I'm getting is:

Script failed for Database 'DatabaseName'.


Maybe this can help:

Message="The server principal "SomeName" is not able to access the database "DatabaseName" under the current security context."


Error details:

Microsoft.SqlServer.Management.Smo.FailedOperationException was caught
HelpLink="http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00.3042.00&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Script+Database&LinkId=20476"
Message="Script failed for Database 'Basic3'. "
Operation="Script"
ProductName="Microsoft SQL Server"
Source="Microsoft.SqlServer.Smo"
StackTrace:
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.ScriptImpl(ScriptingOptions so) at Microsoft.SqlServer.Management.Smo.Database.Script(ScriptingOptions scriptingOptions) at DatabaseCompare.DBHelper.dataBaseToFile(String path) in C:\Documents and Settings\thulanim\My Documents\Visual Studio 2008\Projects\DatabaseCompare\DatabaseCompare\DBHelper.vb:line 93
InnerException: Microsoft.SqlServer.Management.Smo.FailedOperationException
HelpLink="http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00.3042.00&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Script+Database&LinkId=20476"
Message="Script failed for Database 'databaseName. "
Operation="Script"
ProductName="Microsoft SQL Server"
Source="Microsoft.SqlServer.Smo"
StackTrace:
at Microsoft.SqlServer.Management.Smo.Scripter.ScriptWithList(DependencyCollection depList, SqlSmoObject[] objects) at Microsoft.SqlServer.Management.Smo.Scripter.ScriptWithList(Urn[] urns, SqlSmoObject[] objects) at Microsoft.SqlServer.Management.Smo.Scripter.Script(Urn[] urns, SqlSmoObject[] objects) at Microsoft.SqlServer.Management.Smo.Scripter.Script(SqlSmoObject[] objects) at Microsoft.SqlServer.Management.Smo.SqlSmoObject.ScriptImplWorker(ScriptingOptions so) at Microsoft.SqlServer.Management.Smo.SqlSmoObject.ScriptImpl(ScriptingOptions so)
InnerException: Microsoft.SqlServer.Management.Common.ExecutionFailureException
Message="An exception occurred while executing a Transact-SQL statement or batch."
Source="Microsoft.SqlServer.ConnectionInfo"
StackTrace:
at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType) at Microsoft.SqlServer.Management.Smo.ExecuteSql.ExecuteImmediate(String query) at Microsoft.SqlServer.Management.Smo.ExecuteSql.GetDataProvider(StringCollection query, Object con, StatementBuilder sb, RetriveMode rm) at Microsoft.SqlServer.Management.Smo.SqlObjectBase.FillData(ResultType resultType, StringCollection sql, Object connectionInfo, StatementBuilder sb) at Microsoft.SqlServer.Management.Smo.SqlObjectBase.FillDataWithUseFailure(SqlEnumResult sqlresult, ResultType resultType) at Microsoft.SqlServer.Management.Smo.SqlObjectBase.BuildResult(EnumResult result) at Microsoft.SqlServer.Management.Smo.SqlObjectBase.GetData(EnumResult erParent) at Microsoft.SqlServer.Management.Smo.Environment.GetData() at Microsoft.SqlServer.Management.Smo.Environment.GetData(Request req, Object ci) at Microsoft.SqlServer.Management.Smo.Enumerator.GetData(Object connectionInfo, Request request) at Microsoft.SqlServer.Management.Smo.ExecutionManager.GetEnumeratorDataReader(Request req) at Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitChildLevel(Urn levelFilter, ScriptingOptions so, Boolean forScripting) at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.InitializeChildCollection(Boolean refresh) at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.get_Count() at Microsoft.SqlServer.Management.Smo.Database.ScriptCreate(StringCollection createQuery, ScriptingOptions so) at Microsoft.SqlServer.Management.Smo.Scripter.ScriptWithListWorker(DependencyCollection depList, SqlSmoObject[] objects) at Microsoft.SqlServer.Management.Smo.Scripter.ScriptWithList(DependencyCollection depList, SqlSmoObject[] objects)
InnerException: System.Data.SqlClient.SqlException
Class=14
ErrorCode=-2146232060
LineNumber=1
Message="The server principal "SomeName" is not able to access the database "DatabaseName" under the current security context." Number=916

Procedure=""
Server="ServerName"
Source=".Net SqlClient Data Provider"
State=1
StackTrace:
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection) at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj) at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteNonQuery(String sqlCommand, ExecutionTypes executionType)
InnerException:
RBarryYoung
RBarryYoung
SSCrazy Eights
SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)

Group: General Forum Members
Points: 9436 Visits: 9517
well, according to the error, the server logon that you are using, "SomeName" does not have sufficient access to the database to script it.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
thulani.moyana
thulani.moyana
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 64
Thanx for the reply

The Login name is mapped to dbo user wich is has the DB_owner role. Does this role have full acess to the database or I have to add other roles. I tried to add reade role but an error occured.
Any suggestion will be appreciated.
RBarryYoung
RBarryYoung
SSCrazy Eights
SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)

Group: General Forum Members
Points: 9436 Visits: 9517
Check the Login's database mapping at the Server Level:
Expand the Server's Security folder (not the Database's)
, then expand the "Logins" folder
, then select and right-click the "SomeUser" login
, Select "Properties" for the popup menu
On the Properties dialog, select the "User Mapping" view

Now check the list for your database. Unless SomeUser is a SysAdmin, it needs to be mapped to the database here.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
thulani.moyana
thulani.moyana
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 64
Thanks again.

But which server role can script the databse? Especially when it is on remote server. I'm using SMO and VB.net
RBarryYoung
RBarryYoung
SSCrazy Eights
SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)SSCrazy Eights (9.4K reputation)

Group: General Forum Members
Points: 9436 Visits: 9517
thulani.moyana (11/3/2008)
But which server role can script the database? Especially when it is on remote server. I'm using SMO and VB.net


The notion of the server being "remote" is meaningless here. You must have a Login at the server, "remote" or not. That login at the remote server, must be mapped to the database and must have sufficient access to the database to be able to script it.

Usually, you just give db_owner to the scripter, however, you should be able to get away with as little as the "VIEW DEFINITION" permission on the database.

-- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
Proactive Performance Solutions, Inc.
"Performance is our middle name."
thulani.moyana
thulani.moyana
SSC Rookie
SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)SSC Rookie (25 reputation)

Group: General Forum Members
Points: 25 Visits: 64
Thanks a lot.
This makes sense now. I tried to add the "View any definitions" permission to the Login I'm using. I could establish that the Login I'm using cannot add this permission for it's not systemadmin.
Thanks once more.

I would like to become a database administrator. I therefore must have a solid background of SQL. Any Books or links that may be helpful in this regard would be highly appreciated.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search