-
Jul 20th, 2023, 02:02 PM
#1
Thread Starter
PowerPoster
[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.
-
Jul 20th, 2023, 02:28 PM
#2
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;");
-
Jul 20th, 2023, 03:00 PM
#3
Thread Starter
PowerPoster
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.
-
Aug 3rd, 2023, 11:48 AM
#4
Thread Starter
PowerPoster
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.
-
Aug 3rd, 2023, 11:54 AM
#5
Re: [RESOLVED] Connect to sql server from Blazor Server application
@mmock
Sounds like you are well into it and having some success! Good luck!
-
Aug 3rd, 2023, 12:12 PM
#6
Thread Starter
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|