Created
June 13, 2018 08:15
-
-
Save NaserKhoshfetrat/7dd9fc6ac3f90a5070d49aad28dc5b41 to your computer and use it in GitHub Desktop.
c# Execute UDF scalar in sql server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public T ExecUDF_scalar<T>(string UDFName, string[] ParamNames, object[] ParamValues) | |
{ | |
try | |
{ | |
SqlCommand Comm = new SqlCommand(); | |
Comm.Connection = MyConnection; | |
Comm.CommandText = string.Format("SELECT [dbo].[{0}] ({1}) ", UDFName, string.Join(",",ParamNames)); | |
Comm.Parameters.Clear(); | |
for (int i = 0; i < ParamNames.Length; i++) | |
Comm.Parameters.AddWithValue(ParamNames[i], ParamValues[i]); | |
DataSet ds = new DataSet(); | |
MyConnection.Open(); | |
var result = Comm.ExecuteScalar(); | |
return (T) Convert.ChangeType( | |
result.ToString() , typeof(T)); | |
} | |
catch (Exception Ex) | |
{ | |
throw Ex; | |
} | |
finally | |
{ | |
MyConnection.Close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment