Usually, you do not access directly the database from Javascript. You should create some sort of WebAPI and access from there. See Learn in this site links.
As Andrei said, you should create Web API controllers to communicate with a data access layer on the server, and then use AJAX to fetch and send data as JSON which you can work with using JavaScript/jQuery.
You can use Entity Framework, Dapper or plain ADO.NET in your Web API controllers to actually access the SQL Express database. Web API takes care of serialising the data to JSON for you.
Thanks Mike, it has been a while since I have been back at this, but once again you are the best!
I assume I should use core for the API, right? For example, in VS 2017, I would use ASP.Net Core Web Application vs ASP.Net Web Application (.Net Framework).
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Participant
1245 Points
2687 Posts
Best practices way to access SQL Express database with Javascript
Aug 29, 2019 02:03 AM|JAYHAWKER|LINK
I have an HTML page using Javascript and Jquery, and am wanting to access an SQL Express database. What are the best practices way of doing it?
Thanks!
All-Star
120166 Points
27994 Posts
Moderator
MVP
Re: Best practices way to access SQL Express database with Javascript
Aug 29, 2019 04:17 AM|ignatandrei|LINK
Usually, you do not access directly the database from Javascript. You should create some sort of WebAPI and access from there. See Learn in this site links.
All-Star
194444 Points
28074 Posts
Moderator
Re: Best practices way to access SQL Express database with Javascript
Aug 29, 2019 07:17 AM|Mikesdotnetting|LINK
As Andrei said, you should create Web API controllers to communicate with a data access layer on the server, and then use AJAX to fetch and send data as JSON which you can work with using JavaScript/jQuery.
You can use Entity Framework, Dapper or plain ADO.NET in your Web API controllers to actually access the SQL Express database. Web API takes care of serialising the data to JSON for you.
https://docs.microsoft.com/en-gb/aspnet/core/web-api/?view=aspnetcore-2.2
Participant
1245 Points
2687 Posts
Re: Best practices way to access SQL Express database with Javascript
Aug 29, 2019 03:15 PM|JAYHAWKER|LINK
Thanks Mike, it has been a while since I have been back at this, but once again you are the best!
I assume I should use core for the API, right? For example, in VS 2017, I would use ASP.Net Core Web Application vs ASP.Net Web Application (.Net Framework).
All-Star
194444 Points
28074 Posts
Moderator
Re: Best practices way to access SQL Express database with Javascript
Aug 30, 2019 06:31 AM|Mikesdotnetting|LINK
Yes, I would recommend using .NET Core for new projects.
Participant
1245 Points
2687 Posts
Re: Best practices way to access SQL Express database with Javascript
Aug 30, 2019 05:28 PM|JAYHAWKER|LINK
Is there a favorite sample/tutorial you have? A full example, pretty easy to follow that steps all the way through?
Thanks again!
Contributor
3370 Points
1409 Posts
Re: Best practices way to access SQL Express database with Javascript
Sep 02, 2019 10:21 AM|samwu|LINK
Hi JAYHAWKER,
The tutorial link has been provided above.
Create web APIs with ASP.NET Core
you can also refer to this link:
Creating Web API in ASP.NET Core 2.0
https://www.c-sharpcorner.com/article/create-webapi-with-dotnet-core/
Best regards,
Sam
Participant
1245 Points
2687 Posts
Re: Best practices way to access SQL Express database with Javascript
Sep 02, 2019 01:45 PM|JAYHAWKER|LINK
Thanks Sam, that is what I was looking for.