i have a scalar function "[finance_dep].[sum_view1]" in sql server :
- ALTER function [finance_dep].[sum_view1] (@dt1 datetime, @dt2 datetime )
- returns decimal
- as begin
- Declare @d decimal
- set @d=(select sum([amount_dnr]) from [finance_dep].[view1] where [wsl_date] between @dt1 and @dt2)
- return @d
- end
when i calling it from c#:
- public static decimal sum_v(DateTime @dt1, DateTime @dt2)
- {
- return exscalarfunction("select cast(finance_dep.sum_view1(@dt1,@dt2) as decimal) ", CommandType.Text,
- CreateParam("@dt1", SqlDbType.DateTime, @dt1),
- CreateParam("@dt2", SqlDbType.DateTime, @dt2));
- }
- protected static decimal exscalarfunction(string querystoredprocedure, CommandType type, params SqlParameter[] param)
- {
- cn.Open();
-
- decimal f;
- cmd = new SqlCommand(querystoredprocedure, cn);
- cmd.CommandType = type;
- cmd.Parameters.AddRange(param);
-
- f = (decimal) cmd.ExecuteScalar();
-
- cn.Close();
- return f;
- }