How to connect to database? WPF

Hi, I was searching how to connect to database, but I couldn't find anything. I mean to connect non-local host. I have alredy created for my web and I would like to connect with WPF. Any ideas?
17 Replies
leowest
leowest9mo ago
normally you dont connect WPF directly to database specially if you're delivering that code to other users you would have a middle man doing the communication for u i.e.: as asp.net core api that your WPF communicates with via say HttpClient or other means so WPF never talks directly with the database for security reasons there are other reason why u would not do that as well such as race condtions among other things
WPF <=> API => DB
WPF <=> API => DB
does that make sense to u?
♡Mr. L0stík♡
♡Mr. L0stík♡OP9mo ago
Well. So how to connect to use database by your way how to do it? Do you have any documentation for it? And I was working with PHP and I know how SQL injection works. So easy to pretend
leowest
leowest9mo ago
its not my way of doing it its how its generally done
♡Mr. L0stík♡
♡Mr. L0stík♡OP9mo ago
Okay
leowest
leowest9mo ago
guess u never heard of parametized queries? 🙂 anyway have u ever used HttpClient? just a plain simple example
♡Mr. L0stík♡
♡Mr. L0stík♡OP9mo ago
What do you mean HttpClient?
leowest
leowest9mo ago
var client = new HttpClient();
var result = await _httpClient.GetFromJsonAsync<List<WeatherForecast>>("http://10.0.0.1:5213/WeatherForecast").ConfigureAwait(false);
var client = new HttpClient();
var result = await _httpClient.GetFromJsonAsync<List<WeatherForecast>>("http://10.0.0.1:5213/WeatherForecast").ConfigureAwait(false);
this is making a request to a website in my local network as an example to reach the api WeatherForecast which gives me some structured data as response that api gets the data I have in my database so if you're using php, that would be your php proving that information is your website n php?
♡Mr. L0stík♡
♡Mr. L0stík♡OP9mo ago
Okay thanks for idea. I'll see what I can find Yes
leowest
leowest9mo ago
let me see if I can find some resources to get u started ok this is a very simplestic example very barebones just to give u an idea https://medium.com/@miladev95/how-to-make-crud-rest-api-in-php-with-mysql-5063ae4cc89 ofc u can implement authentication and other things so that only authenticated users can access it so imagine ur site is http://helloworld.com and your api is on the folder called api when u call http://helloworld.com/api/ using a GET request it would give u all the books when u call it with a POST request it would be expecting u to send book information to it to add a new book when u call PUT to update a book and respectively when u call DELETE information to delete a book so given my example above
var client = new HttpClient();
var result = await _httpClient.GetFromJsonAsync<List<Books>>("http://helloworld.com/api/").ConfigureAwait(false);
var client = new HttpClient();
var result = await _httpClient.GetFromJsonAsync<List<Books>>("http://helloworld.com/api/").ConfigureAwait(false);
my result would be the list of books and with that data u can populate your WPF app $php
leowest
leowest9mo ago
I would advise u to ask about php API in one of those guilds for a better tutorial because I haven't touched php in ages but the idea is basically the same in asp.net or php if something felt confusing feel free to ask
♡Mr. L0stík♡
♡Mr. L0stík♡OP9mo ago
Okay thanks. I will find some video that explains all of it. I'm wondering about doing private well secured SQL server which will work for both of them and no need for php api. I would like to do something like unity. You can login in their webpage and also in their unity hub. I have no idea how their unity hub works with databases. But you know what I mean right?
leowest
leowest9mo ago
well the problem is u would have to ship the user and password credentials of your database into to all your users and at this point its no longer secure their hub works in the same manner thye have a middle man that deliveries the information from the database you never have direct accsss to the database from their application simple because their app does not have that access so WPF asks information from website, website check database database replies to website with what it asked, website then sends it back to WPF
leowest
leowest9mo ago
No description
leowest
leowest9mo ago
this is a bit too much for an example but its essentially this as u can see u have separate layers between the application that is handed to the user and the database
♡Mr. L0stík♡
♡Mr. L0stík♡OP9mo ago
Okay. I have just one question. For example I would like to have only WPF and not website. But I need middle man so in this case I can do a blank website which will work as middle man? No need for login or something?
leowest
leowest9mo ago
that is what the API is for the API is not a full website its just a url that deliveries data in simple term as an example https://jsonplaceholder.typicode.com/todos this is an API that replies with a todo list but if u access https://jsonplaceholder.typicode.com/todos/1 you can get an specific todo this is just a dummy api for testing but I hope it gives u an idea and the API can have login if u want as well as in u send credentials and it checks the credentials and only functions if the credentials are valid
Want results from more Discord servers?
Add your server