IronPython SqlClient CommandType?

时间:2014-03-31 19:56:38

标签: .net ironpython sqlclient

我正在尝试从iron python运行存储过程,但无法设置CommandType:

import clr
clr.AddReference('System')
clr.AddReference('System.IO')
clr.AddReference('System.Data')
from System import Console as cns
from System.Data import SqlClient as sql

我已经测试了Sql连接的代码并运行一个简单的select查询来验证我是否可以获得一个有效的连接,所以它被传递给这个函数为''con''

def get_cols_for(con, table):
    cols = {}
    cmd = sql.SqlCommand("sp_columns", con)
    cmd.Parameters.Add(sql.SqlParameter("@table_name", "EQUIPMENT")) 
    sp = sql.SqlCommand.CommandType.StoredProcedure
    rdr = cmd.ExecuteReader()
    while rdr.Read():
        cols[str(rdr[3])]=(str(rdr[5]),int(rdr[7])) # ColumnName @ 3, TypeName @ 5
    rdr.Close()
    return cols

intellisense自动将StoredProcedure建议为CommandType成员,所以我很确定它实际上存在,但是当我运行此代码时,我得到以下抱怨:

{“'getset_descriptor'对象没有属性'StoredProcedure'”}

当我执行 sp = sql.SqlCommand.CommandType.StoredProcedure

行时

如何设置CommandType?

顺便说一句,如果我只是使用这个表单,它可以正常工作:

 cmd = sql.SqlCommand("exec sp_columns @table_name='%s'" % table, con)

1 个答案:

答案 0 :(得分:1)

似乎没有导入。 如果您通过

导入它
from System.Data import CommandType as cmdType

它有效:

>>>cmdType.__doc__

返回:

  

指定如何解释命令字符串。枚举   CommandType,值:StoredProcedure(4),TableDirect(512),Text(1)

另请注意,根据msdn,您还应将CommandText属性设置为存储过程的名称。