0

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)
0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, thanks tobsen, I had tried to import CommandType from System.Data.SqlCommand since it appears in that library, but it does work the way you import it. BTW, the CommandText is set by the SqlCommand constructor, so there's no need to set it separately. I could have said cmd = sql.SqlCommand(con), then set it as the next step, e.g. cmd.CommandText="sp_columns" too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.