multiple connections

Posts   
 
    
nhk
User
Posts: 1
Joined: 19-Jul-2012
# Posted on: 19-Jul-2012 15:41:45   

Using LLBLGEN 2.6, .NET 3.5, self-servicing, C#.

While I've read quite a bit about how to make connections with more than one database, I'm new enough to using llblgen that I'm wondering if anyone can point to a simple example that includes all the pieces (.config, C# calls) needed to talk to multiple databases in a single application.

My situation:
1) two identically structured (and named) tables on two different servers with different db names 2) user attempts logs in with credentials 3) need to check credentials to see if user is in either table, and if a match is found, to have access to the info for their entry in whichever table they're found in. Also, need to know if they're found in both, as that would constitute an error.

I know that you can specify different catalog names on a per call basis, that you can do overrides of catalog names, and that you maybe should use adapter instead of self-servicing, but am new enough to llblgen pro to not be 100% sure how this looks in actual code. Is there any simple example that would show how my situation would be wired together? Not a solution to my issue, necessarily, but just C# code and config that shows how to override catalog names to be able to touch two tables on two separate servers under different db names.

Thanks in advance.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 20-Jul-2012 07:27:00   

An application of this is not of much help, but if you really want it we can make you one. However I can explain how to achieve what you want.

Using the catalog overwrites in the config won't help you here because you want to change from catalog from catalog dynamically in your app. So you have to make it in your code. This is a very simplistic factory class that could help you:

public static class AdapterFactory
{
    public static IDataAccessAdapter GiveMeAnAdapter(int server)
    {
        var catalogOverwrite = new CatalogNameOverwriteHashtable();

        // default connection string
        var connectionString = "data source=.;initial catalog=master;integrated security=SSPI;persist security info=False;packet size=4096";

        switch (server)
        { 
            case 2:
                catalogOverwrite.Add("Northwind", "NW2");
                connectionString = "data source=.;initial catalog=master;integrated security=SSPI;persist security info=False;packet size=4096";
                break;

            // ...
        }

        return new DataAccessAdapter(connectionString, false, catalogOverwrite, null);
    }
}

Then, when the user logins:

var filter = new RelationPredicateBucket(UserFields.Nick == txtNick.Text);

var usersMatchesFromServer1 = new EntityCollection<UserEntity>();
using (var adapter = AdapterFactory.GiveMeAnAdapter(1))
{
    adapter.FetchEntityCollection(usersMatchesFromServer1, filter);
}

var usersMatchesFromServer2 = new EntityCollection<UserEntity>();
using (var adapter = AdapterFactory.GiveMeAnAdapter(2))
{
    adapter.FetchEntityCollection(usersMatchesFromServer2, filter);
}

if (usersMatchesFromServer1.Count > 0 && usersMatchesFromServer2.Count > 0)
{
     // notify error
}

var serverToUseOnThisSession = usersMatchesFromServer1.Count > 0 ? 1 : 2;

// ... later ...
using (var adapter = AdapterFactory.GiveMeAnAdapter(serverToUseOnThisSession))
{
    // ...
}
David Elizondo | LLBLGen Support Team