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
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
does that make sense to u?
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
its not my way of doing it its how its generally done
Okay
guess u never heard of parametized queries?
🙂
anyway
have u ever used HttpClient?
just a plain simple example
What do you mean HttpClient?
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?
Okay thanks for idea. I'll see what I can find
Yes
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
my result would be the list of books
and with that data u can populate your WPF app
$php
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
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?
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
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
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?
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