Select single value w.o. using PK

Posts   
 
    
VbMan
User
Posts: 6
Joined: 28-Nov-2006
# Posted on: 10-Feb-2007 05:32:39   

Ok,

I am racking my brain here and I have seen examplein C# but can not seem to translate it into Vb.Net. I need to extract a single value from a table in the db and I want to do it withou have to create a stored procedure everytime I need to do this. Can you give me some example code?

I am using .Net 1.1 and VB syntax.

Feeling like a newbie here and everthing I have tried fails. I am reading mounds of help documentation and forum postings just to get a simplistic funciton done.

Thank you in advance, ~Mann.

PS. While I am asking.. how about searching for any enitity without using the primary key?

jmeckley
User
Posts: 403
Joined: 05-Jul-2006
# Posted on: 10-Feb-2007 15:00:47   

It's been awhile since I've worked with VB or .Net 1.1 syntax, so I won't be able to help you there.

To select a single entity without using PK use fetchusingUniqueConstraint. If you don't have a unique constraint on the targeted entity you'll need to fetchCollection and select the 1st item in the collection.

Use GetScalar() to exact single values from a database.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 10-Feb-2007 15:04:57   

VbMan wrote:

Ok,

I am racking my brain here and I have seen examplein C# but can not seem to translate it into Vb.Net. I need to extract a single value from a table in the db and I want to do it withou have to create a stored procedure everytime I need to do this. Can you give me some example code?

I am using .Net 1.1 and VB syntax.

Feeling like a newbie here and everthing I have tried fails. I am reading mounds of help documentation and forum postings just to get a simplistic funciton done.

You mean, retrieving a scalar value? It would really help if you could explain what you're using: selfservicing or adapter. Also, if you have a C# example, please post that. The manual contains VB.NET examples for EVERY C# example.

In Selfservicing you should use a GetScalar call on a collection, e.g.: Selfservicing:


Dim orderDetails As New OrderDetailsCollection()
Dim productPriceExpression As IExpression = New Expression( _
    OrderDetailsFields.Quantity, ExOp.Mul, OrderDetailsFields.UnitPrice))
Dim filter As IPredicate = New FieldCompareValuePredicate(OrderDetailsFields.OrderId, ComparisonOperator.Equal, 10254)
Dim orderPrice As Decimal = CDec(orderDetails.GetScalar(OrderDetailsFieldIndex.OrderId, _
    productPriceExpression, AggregateFunction.Sum, filter))

Adapter:


Dim adapter As DataAccessAdapter = New DataAccessAdapter()
Dim productPriceExpression As IExpression = New Expression( _
    OrderDetailsFields.Quantity, ExOp.Mul, OrderDetailsFields.UnitPrice)
Dim filter As IPredicate = New FieldCompareValuePredicate(OrderDetailsFields.OrderId, Nothing, ComparisonOperator.Equal, 10254)
Dim orderPrice As Decimal = CDec(adapter.GetScalar(OrderDetailsFields.OrderId, _
        productPriceExpression, AggregateFunction.Sum, filter))

PS. While I am asking.. how about searching for any enitity without using the primary key?

Use a filter on a collection fetch. So use a predicate for the filter which should be the filter for the entity to find, and then fetch a collection with that filter.

Frans Bouma | Lead developer LLBLGen Pro
VbMan
User
Posts: 6
Joined: 28-Nov-2006
# Posted on: 10-Feb-2007 22:44:10   

After some more research I was finally able to get this to work to retrieve a single value from a table in the db: (I am using Vb.Net 1.1, Adapter)

Dim adapter As New DL.DatabaseSpecific.DataAccessAdapter Dim filter As IPredicate = New FieldCompareValuePredicate(ProvidersFields.ProviderStrId, Nothing, ComparisonOperator.Equal, strDomainID) Dim strDomain As String = CStr(adapter.GetScalar(ProvidersFields.ProviderSmsdomain, Nothing, AggregateFunction.None, filter)) adapter.CloseConnection() adapter = Nothing

    Return strDomain

:: Is this the fastest/scalable method? I will be using this method over and over again.

Next I will be needing to retrieve an enitiy without using the PK. In SQL it would be: Select [domain] where DomainStrId = "www.someDomain.com"

Retrieiving from a primary key is real easy, that is if you know the primary key, it would be great if you could return values from other fields just as esily. This is not as straight foward as I initially thought it would be.

Again, thank you for your time. ~Mann.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 12-Feb-2007 07:37:08   

Is this the fastest/scalable method? I will be using this method over and over again.

Yes that's the fastest method.

Next I will be needing to retrieve an enitiy without using the PK. In SQL it would be: Select [domain] where DomainStrId = "www.someDomain.com"

I'll re-use jmeckley words:

To select a single entity without using PK use fetchusingUniqueConstraint. If you don't have a unique constraint on the targeted entity you'll need to fetchCollection and select the 1st item in the collection.