StrataFrame Forum

Executing Scalar-Valued Function from VB.NET

http://forum.strataframe.net/Topic10459.aspx

By StarkMike - 7/19/2007

How do I execute a sql server scalar-valued function from vb.net? Is it just like a stored procedure?
By Greg McGuffey - 7/19/2007

How do I execute a sql server scalar-valued function from vb.net? Is it just like a stored procedure?




If I understand you correctly, you want to do something like pass in a PK for a table and get the value of one column or get the count of rows that match some criterion. I.e. get a single value back from SQL Server. You'd do this in the appropriate BO. Here is an example



Public Sub GetCustomerCountByCounty(county As String) As Integer

Dim count As Integer

Using cmd As New SqlCommand()

cmd.CommandText = "Select Count(*) From Customers Where County = @county"

cmd.Parameters.Add("@county",varchar,50)

cmd.Parameters("@county").Value = county

Dim retObj As Object = cmd.ExecuteScalar(cmd)

If retObj IsNot Nothing AndAlso retObj IsNot DBNull.Value Then

count = CType(retObj,Integer)

End If

End Using

Return count

End Sub




Take note that I return the value from SQL to an Object, not an integer. In this case, it would likely be safe-ish to return to an integer, as count() return an integer. But if this had been a column, then the return could be Nothing (null is C#) if no record was found, or it could be DBNull.Value if the record was found, but the column was NULL.



I just typed this in, so there may be some errors. I use intellisense a lot BigGrin I know the enum for the Parameters.Add is wrong, but you should get the idea.
By StarkMike - 7/19/2007

Thanks Greg. What I mean is if I create a scalar valued function in SQL Server... how would I execute that function and have its result returned to me in VB? Cool
By Greg McGuffey - 7/19/2007

Ah...well I hope my previous example helps someone else then.



That is a very good question. I've never tried that before, calling a function from client code. My guess would be that you'd just use the above code, but for the CommandText use something like:



cmd.CommandText = "Select fn_GetCustomersByCounty(@county)"




where fn_GetCustomersByCounty is your scalar SQL function. That should work.
By StarkMike - 7/20/2007

Thats awesome! Thanks Greg... that worked. ;-)
By StrataFrame Team - 7/20/2007

Yep, Greg's solution works (just remember to either set the Connection property on the command or call ExecuteScalar(cmd) through a business object) Wink  However, if you ever need to return a value from a stored procedure (or multiple values), you can also use output parameters.  After you call ExecuteScalar() or ExecuteNonQuery(), the parameters labeled as Direction = Output will have the values assigned by the server.  Just FYI.
By StrataFrame Team - 7/20/2007

Greg's solution also works with more than just scalar values... you can use it with full record sets as well.
By Greg McGuffey - 7/20/2007

StarkMike (07/20/2007)
Thats awesome! Thanks Greg... that worked. ;-)




BigGrin Glad you got it working!
By Greg McGuffey - 7/20/2007

Ben Chase (07/20/2007)
Greg's solution also works with more than just scalar values... you can use it with full record sets as well.




I'm not following you Ben. How would you use ExecuteScalar with a "full record sets"? Doesn't it just return a single value?