.net - SQL UDT Type parameter pass in C# -
i have sql udt type
create type [dbo].[udtname] nvarchar(50) null go
and using in procedure insert data db. procedure this
create procedure [dbo].[proc_name] ( @param1 int, @param2 int, @param3 udtname, @param4 udtcomment, @param5 udttype = '' output, @param6 udttype = '' output )
when calling procedure c# code, udt type data not saving in table.
my c# code:
private idictionary<string, string> runquery(string procname, list<parameter> input) { foreach (parameter inputpair in input) { sqlparameter sp = new sqlparameter("@" + inputpair.name , inputpair.type); // have tried sqldbtype varchar, nvarchar sp.direction = system.data.parameterdirection.input sp.sqldbtype = sqldbtype.udt; // have if condition around check if udt type add udttypename sp.udttypename = "udtname"; // have tried dbo.udtname sp.size = inputpair.size; //setting in case of varchar sp.value = inputpair.value; cmd.parameters.add(sp); } cmd.commandtype = commandtype.storedprocedure; param1.parametername = "@param5"; param1.sqldbtype = system.data.sqldbtype.varchar; param1.size = 255; param1.direction = system.data.parameterdirection.output; cmd.parameters.add(param1); _reader = cmd.executereader(); }
parameter class
public class parameter { public string name { get; set; } public object value { get; set; } public object type { get; set; } public bool isudt { get; set; } public int size { get; set; } public bool udttypename { get; set; } }
but not inserting data db , throwing following exception:
result message: system.exception : specified type not registered on target server.system.string, mscorlib, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089.
if using varchar sqldbtype in place of udttype sqldbtype in code, running not inserting data tables.
i did research everywhere people using table value pair base udt type.
note: don't have privileges alter in sql db.
edit
i tried datatable also
my code:
datatable datatable = new datatable("udtname"); datatable.columns.add(); datatable.rows.add("my_value"); var inputparam = cmd.parameters.addwithvalue("@param3", datatable); inputparam.sqldbtype = sqldbtype.structured; inputparam.direction = system.data.parameterdirection.input;
it showing below error:
result message: system.exception : operand type clash: table type incompatible nvarchar(50)
you need correctly specify 2 types (sp.sqldbtype
, sp.udttypename
), name , value.
it looks either setting wrong value or incorrect sp.udttypename
.
check way creating sqlparameter
:
sqlparameter sp = new sqlparameter("@" + inputpair.name , inputpair.type);
the problem here inputpair.type
of type object.
so following constructor called:
public sqlparameter( string parametername , object value )
instead of other might expect:
public sqlparameter( string parametername , sqldbtype dbtype )
your code should instead:
sqlparameter sp = new sqlparameter("@" + inputpair.name , value); sp.sqldbtype = sqldbtype.udt; sp.udttypename = "udtname";
Comments
Post a Comment