如何在ironpython中为Microsoft.SqlServer.SMO.Scripter.Script类定义参数

发布于 2024-10-07 14:16:47 字数 2223 浏览 4 评论 0原文

我想使用下面的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与酒说心事 2024-10-14 14:16:47

我找到了解决方案:

arg=System.Array[SMO.SqlSmoObject]([table])

完整的脚本如下所示:

import sys    
import clr    
# import .NET Array
import System.Array

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:    
        # create a .NET Array as an argument for the scripter
        arg=System.Array[SMO.SqlSmoObject]([table])
        script = scripter.Script(arg)
        #output script
        for line in script:
            print line

I found the solution:

arg=System.Array[SMO.SqlSmoObject]([table])

The full script looks like:

import sys    
import clr    
# import .NET Array
import System.Array

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:    
        # create a .NET Array as an argument for the scripter
        arg=System.Array[SMO.SqlSmoObject]([table])
        script = scripter.Script(arg)
        #output script
        for line in script:
            print line
手心的海 2024-10-14 14:16:47

从未使用过 IronPython 或 SMO,但看起来它需要某种类型的集合。您是否尝试过:

scripter.Script(database.Tables)

而不是一次编写一张表的脚本?

Never used IronPython or SMO, but it looks like it expects a collection of some sort. Have you tried:

scripter.Script(database.Tables)

instead of scripting one table at a time?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文