I want call function created in SQL Server, which receives two parameters and returns an integer. When I call stored procedure, I use the following code:
sqlcmd.CommandType = CommandType.StoredProcedure
sqlcmd.CommandText = "PROCEDURE_NAME"
sqlcmd.Parameters.Add(New SqlClient.SqlParameter("@param1", Utilities.NothingToDBNull(user)))
sqlcmd.Parameters.Add(New SqlClient.SqlParameter("@param2", Utilities.NothingToDBNull(password)))
da = New SqlClient.SqlDataAdapter()
da.SelectCommand = sqlcmd
table = New DataTable()
da.Fill(table)
In this case I have a table returned by the stored procedure. What changes if I want use a function that returns a scalar value instead of stored procedure?
You can’t call that function directly, only
StoredProcedure,Text(query), andTableDirectare allowed. Since you are already exposed with stored procedure, why not create a procedure that has the function on it?In your C# code, you can use the
ExecuteScalarof your command objectYour stored procedure should look like this now,