Question

I'm trying to run a stored proc from iron python but having trouble setting the 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

I've tested code for the Sql connection and run a simple select query to verify that I can get a working connection, so that's passed into this function as ''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

The intellisense suggests StoredProcedure as a CommandType member automatically, so I'm pretty sure it's actually there, but when I run this code I get the following complaint:

{"'getset_descriptor' object has no attribute 'StoredProcedure'"}

when I execute the line sp = sql.SqlCommand.CommandType.StoredProcedure

How does one go about setting the CommandType?

Incidentally, if I just use this form, it works ok:

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

Solution

It seems it isn't imported. If you import it via

from System.Data import CommandType as cmdType

it works:

>>>cmdType.__doc__

returns:

Specifies how a command string is interpreted. enum CommandType, values: StoredProcedure (4), TableDirect (512), Text (1)

Please also note that you, according to msdn, should also set the CommandText property to the name of the stored procedure.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top