Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Scripting Remote Database using SMO in .net Expand / Collapse
Author
Message
Posted Thursday, October 30, 2008 8:06 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 4, 2009 1:35 AM
Points: 19, 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)



Post #594345
Posted Thursday, October 30, 2008 10:40 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,294, Visits: 9,480
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."
Post #594537
Posted Friday, October 31, 2008 12:36 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 4, 2009 1:35 AM
Points: 19, 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:
Post #594789
Posted Friday, October 31, 2008 6:31 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,294, Visits: 9,480
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."
Post #594899
Posted Friday, October 31, 2008 6:37 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 4, 2009 1:35 AM
Points: 19, 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.
Post #594904
Posted Friday, October 31, 2008 7:58 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,294, Visits: 9,480
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."
Post #594989
Posted Monday, November 3, 2008 1:07 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 4, 2009 1:35 AM
Points: 19, 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
Post #595658
Posted Monday, November 3, 2008 7:09 AM


SSCrazy Eights

SSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy EightsSSCrazy Eights

Group: General Forum Members
Last Login: Thursday, June 5, 2014 10:54 AM
Points: 9,294, Visits: 9,480
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."
Post #595780
Posted Monday, November 3, 2008 8:11 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Wednesday, November 4, 2009 1:35 AM
Points: 19, 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.
Post #595838
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse