Using C# classes/structs in SQL Server -
is there way can declare instances of .net class or struct in sql server stored procedure and/or use them parameters in function?
i have .net method takes 2 instances of unitofmeasure
class , converts value 1 unit other.
my .net class looks following. i've omitted lot of code sake of brevity...
public class unitofmeasure { public static unitofmeasure gram {get;} = new unitofmeasure("gram","g",unittype.mass, 0.001, 0); public static unitofmeasure kilogram {get;} = new unitofmeasure("kilogram","kg",unittype.mass,1,0); ... public static ienumerable<unitofmeasure> namedunits {get;} = new list<unitofmeasure>(){gram, kilogram, ...}; public string name {get; private set;} public string symbol {get; private set; } public unittype {get; private set;} public double standardunits {get; private set} public double standardoffset {get; private set} public unitofmeasure(string name, string symbol, unittype type, double standardunits, double standardoffset) { ... } public static double? convert(unitofmeasure fromunit, unitofmeasure tounit, double? value) { double? standardvalue = (value * fromunit.standardunits) + fromunit.standardoffset; double ret = (standardvalue - tounit.standardoffset)/ tounit.standardunits; return ret; } public static double convert(string fromunit, string tounit, double? value) { var unit1 = namedunits.single(unit => unit.name == fromunit); var unit2 = namedunits.single(unit => unit.name == tounit); return convert(unit1, unit2, value); } }
i've been able map convert (by name) method udf in database, works when convert from/to units in "named units" collection.
udf_convertunits('gram', 'kilogram', 200); //returns 0.2
i able define custom units within sql , convert between them...
declare @usd = createunit('us dollar', 'usd', 'currency', 100, 0); declare @eur = createunit('euro', 'eur', 'currency', 88.9, 0); udf_convertcustomunits(@usd, @eur, 200); // returns 177.8
Comments
Post a Comment