Results 1 to 6 of 6

Thread: [RESOLVED] Connect to sql server from Blazor Server application

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,484

    Resolved [RESOLVED] Connect to sql server from Blazor Server application

    I am working on a new Blazor Server application and it's my first one so I am learning. I want to connect to my database. But just to have some fun, not for real, so it doesn't need to be all that secure. Can't I just add a connection string to appsettings.json and be on my merry way? I coded a cute little page with a grid that displays data for *one* of our customers. I hardcoded it all 🙁. I would like to switch to a new customer (that should also come from our database so I can list them all) and load new data. Can't I just test this without getting all serious about database access? Thanks!
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,809

    Re: Connect to sql server from Blazor Server application

    This is my typical setup.

    Step 1 is to setup a connectionStrings element in your Web.config and set the configSource to an external file:
    Code:
    <configuration>
        ...
        <connectionStrings configSource="connectionStrings.config" />
    </configuration
    Step 2 is to create the external file where the build action is content and the copy to output directory is copy always. The contents will be an empty connection string entry:
    Code:
    <connectionStrings>
        <add name="MyDataModel" connectionString="data source=change-me;initial catalog=change-me;user id=change-me;password=change-me;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
    </connectionStrings>
    Step 3 is to build the project so that the file gets added to the .csproj file.

    Step 4 is optional if you are using Git. That is to add the external connection strings file to the .gitignore:
    Code:
    connectionStrings.config
    Step 5 is to update the connection string value so that you replace all the "change-me" values to be your actual values.

    Step 6 is to setup the unity factory. Create a new class with a static method which returns a UnityContainer that looks like this:
    Code:
    public class UnityFactory
    {
        private static UnityContainer _unityContainer = null;
    
        static public UnityContainer Instance()
        {
            if(_unityContainer == null)
            {
                var container = new UnityContainer();
                _unityContainer = container;
    
                // this assumes you are using some ORM like PetaPoco or Entity Framework
                container.RegisterType<IDatabase, Database>(new PerThreadLifetimeManager(), new InjectionConstructor("MyDataModel")); // MyDataModel will be the connection string name
            }
            Return _unityContainer;
        }
    
    }
    Now when you need to get your database class, all you need to do is get it from your container and you can do your operations, e.g.:
    Code:
    var container = UnityFactory.Instance();
    var database = container.Resolve<IDatabase>();
    var records = database.Query<MyTableModel>("SELECT * FROM MyTable;");
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,484

    Re: Connect to sql server from Blazor Server application

    Thank you. In the meantime I found this which seems very helpful. It's slow and detailed which is where I am at the moment. 32:24 into this video he discusses data access.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  4. #4

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,484

    Re: Connect to sql server from Blazor Server application

    That youtube video I previously cited was very helpful. I am using Dapper at this time, while developing.
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: [RESOLVED] Connect to sql server from Blazor Server application

    @mmock

    Sounds like you are well into it and having some success! Good luck!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,484

    Re: [RESOLVED] Connect to sql server from Blazor Server application

    Phew! I saw your reply and was afraid you were going to point out something I did wrong!
    Thanks!
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width