C
C#β€’4w ago
Gipper

Easiest and simplest way to insert a Microsoft SQL Server DB into an app saving data in csv files?

I got a solution with several projects, most of them are console applications, one is a gRPC server. I'm currently saving data in a sequence of csv files and I would like to do the same in a simple database with as many tables as I have csv files (about 6/7 of them). What would be the best way to inject a database in it? I say MSSQL Server only because that's the one I got setup on my system, could be another one hypothetically. So long as it's quick and easy πŸ™‚
13 Replies
kurumi
kurumiβ€’4w ago
the question is what is the best way to insert data to your database and share it with multiple projects? If yes, you should use SQL Bulk insert operations (or COPY for PostgreSQL). Also, some databases provide own mechanisms to upload data directly to tables using popular data formats like CSV.
Gipper
Gipperβ€’4w ago
No, I mean actually create a database and connect it to the solution and subsequently be able to access and introduce data into it through C# code I'd rather not write SQL if I can avoid it the data itself thta I have right now doesn't really matter, it only matters as a display that the system is working as intended and is able to generate a preserve a state
kurumi
kurumiβ€’4w ago
if you wanna avoid SQL, you can try EFCore, it supports many popular providers, data can be insert with AddRange method but then you need to parse CSV into your collection and do inserts manually this is not the best way if you have HUGE data
Gipper
Gipperβ€’4w ago
When I try to add an EFCore project to my solution in MS Visual Studio it's not an option there...
kurumi
kurumiβ€’4w ago
but I guess there are some libraries you can use for bulk operations
Gipper
Gipperβ€’4w ago
and it's not at all huge data
kurumi
kurumiβ€’4w ago
why?
Gipper
Gipperβ€’4w ago
it's 🀏 data probably cause it's not a web app? i'm speculating
kurumi
kurumiβ€’4w ago
EFCore doest require you web app, you can do it from console
kurumi
kurumiβ€’4w ago
install this nuget package and set up your host https://www.nuget.org/packages/microsoft.extensions.hosting
Microsoft.Extensions.Hosting 8.0.0
Hosting and startup infrastructures for applications.
kurumi
kurumiβ€’4w ago
here is an example how you can setup host in console application and have IServiceCollection https://github.com/tokKurumi/RestApiChallenge/blob/master/MultithredRest%2FHostDefaults%2FMultithreadRestHost.cs
GitHub
RestApiChallenge/MultithredRest/HostDefaults/MultithreadRestHost.cs...
Challenge to create REST API C# application without ASP.NET Web API - tokKurumi/RestApiChallenge
kurumi
kurumiβ€’4w ago
but it can be done easier without all these host abstractions and technics such as DI, just create instance of DbContext and use it
friedice
friediceβ€’4w ago
I mean what I would do is create a project for the data access layer, create the models of your csv(s) alongside appdbcontext, create the crud repos and inject it accordingly