如何在ironpython中为Microsoft.SqlServer.SMO.Scripter.Script类定义参数
我想使用下面的ironpython代码编写我的数据库对象的脚本:
import sys
import clr
database_name = r'localhost\SQLEXPRESS'
dir_assemblies = r'D:\programfiles\Microsoft SQL Server\100\SDK\Assemblies'
# Import SMO Namespace
sys.path.append(dir_assemblies)
clr.AddReferenceToFile('Microsoft.SqlServer.Smo.dll')
import Microsoft.SqlServer.Management.Smo as SMO
db = SMO.Server(database_name)
scripter = SMO.Scripter(db)
for database in db.Databases:
for table in database.Tables:
# TypeError: expected Array[Urn], got Table
scripter.Script(table)
执行此代码时,出现以下错误:
File "SMOtest2.py", line 18, in <module>
TypeError: expected Array[Urn], got Table
SMO.Scripter.doc为我提供以下信息:
Script(self: Scripter, urns: Array[Urn]) -> StringCollection
Script(self: Scripter, list: UrnCollection) -> StringCollection
Script(self: Scripter, objects: Array[SqlSmoObject]) -> StringCollection
我尝试创建一个数组[Urn ] 或 Array[SqlSmoObject] 但没有任何成功。
有谁知道如何为 SMO.Scripter.Script 类创建正确的参数?
我想用Python编写下面的VB代码。 摘自:http://msdn.microsoft.com /en-us/library/ms162160(v=SQL.90).aspx
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Define a Scripter object and set the required scripting options.
Dim scrp As Scripter
scrp = New Scripter(srv)
scrp.Options.ScriptDrops = False
scrp.Options.WithDependencies = True
'Iterate through the tables in database and script each one. Display the script.
'Note that the StringCollection type needs the System.Collections.Specialized namespace to be included.
Dim tb As Table
Dim smoObjects(1) As Urn
For Each tb In db.Tables
smoObjects = New Urn(0) {}
smoObjects(0) = tb.Urn
If tb.IsSystemObject = False Then
Dim sc As StringCollection
sc = scrp.Script(smoObjects)
Dim st As String
For Each st In sc
Console.WriteLine(st)
Next
End If
Next
I want to script my database objects using the ironpython code below:
import sys
import clr
database_name = r'localhost\SQLEXPRESS'
dir_assemblies = r'D:\programfiles\Microsoft SQL Server\100\SDK\Assemblies'
# Import SMO Namespace
sys.path.append(dir_assemblies)
clr.AddReferenceToFile('Microsoft.SqlServer.Smo.dll')
import Microsoft.SqlServer.Management.Smo as SMO
db = SMO.Server(database_name)
scripter = SMO.Scripter(db)
for database in db.Databases:
for table in database.Tables:
# TypeError: expected Array[Urn], got Table
scripter.Script(table)
When executing this code, I get the following error:
File "SMOtest2.py", line 18, in <module>
TypeError: expected Array[Urn], got Table
The SMO.Scripter.doc gives me the following information:
Script(self: Scripter, urns: Array[Urn]) -> StringCollection
Script(self: Scripter, list: UrnCollection) -> StringCollection
Script(self: Scripter, objects: Array[SqlSmoObject]) -> StringCollection
I tried creating an Array[Urn] or an Array[SqlSmoObject] but without any succes.
Does anyone have an idea how I can create the right argument for the SMO.Scripter.Script Class?
I want to write the VB code below in python.
Taken from: http://msdn.microsoft.com/en-us/library/ms162160(v=SQL.90).aspx
'Connect to the local, default instance of SQL Server.
Dim srv As Server
srv = New Server
'Reference the AdventureWorks database.
Dim db As Database
db = srv.Databases("AdventureWorks")
'Define a Scripter object and set the required scripting options.
Dim scrp As Scripter
scrp = New Scripter(srv)
scrp.Options.ScriptDrops = False
scrp.Options.WithDependencies = True
'Iterate through the tables in database and script each one. Display the script.
'Note that the StringCollection type needs the System.Collections.Specialized namespace to be included.
Dim tb As Table
Dim smoObjects(1) As Urn
For Each tb In db.Tables
smoObjects = New Urn(0) {}
smoObjects(0) = tb.Urn
If tb.IsSystemObject = False Then
Dim sc As StringCollection
sc = scrp.Script(smoObjects)
Dim st As String
For Each st In sc
Console.WriteLine(st)
Next
End If
Next
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了解决方案:
完整的脚本如下所示:
I found the solution:
The full script looks like:
从未使用过 IronPython 或 SMO,但看起来它需要某种类型的集合。您是否尝试过:
而不是一次编写一张表的脚本?
Never used IronPython or SMO, but it looks like it expects a collection of some sort. Have you tried:
instead of scripting one table at a time?