How to use SQL user defined functions in .NET? -


After

I created a scalar function in DB

  Go set to set QUOTED_IDENTIFIER ANSI_NULLS ALTER Function [dbo]. [Fn_GetUserId_Username] (@ User Name varchar (32)) declared @UserId integer selection @UserId = from UserId start int [user] Where Username = @ Username Return @UserId End  

Now I want to run it in my .NET C # or VB.NET code.

I use the unit framework, I tried to mapping the function mapping and I did not get success. Public Integer GetUserIdByUsername {String User Name} {EntityConnection Connection: I do not care to do this with simple DbCommand, the problem is that I get any results (function entities are present in the class) Is = (unit connection) connection; DB CommM = Connection. Store connection Crete Commands (); Com.CommandText = "fn_GetUserId_Username"; Com.CommandType = CommandType.StoredProcedure; Com.Parameters.Add (new SqlParameter ("user name", user name)); If (com.connection.state == connectionstat.closed) com. Connection open (); Try {var result = com.ExecuteScalar (); // always tap) hold (exception e) {} return result; }

Is there a solution? In either C # or VB.NET posts will be welcommed.

Text after "div class =" itemprop = "text">

It looks like right in this case the way one does. NET function is to define and map to your UDF that is to use the functionality of the outline of the unit, but I think I see why you have the results you expect when you use ADO.NET Do not get to do this - you are saying that you are calling a stored procedure, But are you really calling a function?

Try this:

  public int GetUserIdByUsername (string username) {EntityConnection connection = (EntityConnection) connection; DB CommM = Connection. Store connection Crete Commands (); Select Com.CommandText = "dbo.fn_GetUserId_Username (@Username)"; Com.CommandType = CommandType.Text; Com.Parameters.Add (new SqlParameter ("@username", user name)); If (com.connection.state == connectionstat.closed) com. Connection open (); Try {var result = com.ExecuteScalar (); // Reasonably get your value return (integer) result; } Catch (Exception e) {// either take some exception handling code or hold // block or exception bubble out}}  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

Visual Studio 2005: How to speed up builds when a VSMDI is open? -

booting ubuntu from usb using virtualbox -