Scripting Remote Database using SMO in .net

  • 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)

  • We cannot see the error from here. Could you please tell us what it is?

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • 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:

  • well, according to the error, the server logon that you are using, "SomeName" does not have sufficient access to the database to script it.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • 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.

  • 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.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Thanks again.

    But which server role can script the databse? Especially when it is on remote server. I'm using SMO and VB.net

  • 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.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • 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.

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply