How can I connect to an external SQL database using Blazor without using packages (like Entity Framework) - sql

I'm not even sure if this is possible but Google has been unable to help me. It may just be because Blazor is so new. Anyway, I've got a premade database and I want to connect to it directly like how you can open a connection, run some SQL, then close a connection in ASP.NET. I, unfortunately, can't just make a new database using code-first as most tutorials tell you to do.

Two options that spring to mind is the Entity Framework Core (Database First) or Dapper.
I'm actually connecting to an existing database using Dapper in my Blazor projects and there are better Dapper examples/tutorials available however the below is a basic example.
https://github.com/DotNetDublin/BlazorServerSide/tree/main/BlazorServerSide
If you don't want to use either Entity Framework or Dapper you can use ADO.NET.
The below tutorial is for MVC however the code for interacting with the database would be the same. See StudentDataAccessLayer.
https://www.c-sharpcorner.com/article/crud-operations-using-asp-net-core-and-ado-net/

use the ef command Scaffold-DbContext

Related

Thinktecture IdentityManager choosing a db schema

I recently setup a dev site and am using IdentityServer3 with IdentityManager, both from thinktecture, and IdentityManager is designed to create the database for itself, but can be configured to work with an existing db. I was able to get IdentityManager into a local db I had previously created with the default schema, but I would like to switch it to a new schema. Basically the question is that I can't figure out how to set the desired schema in the db in IdentityManager and can anyone in here give any insight?
There are many ways to do this (idsrv3 is very configurable). A common way is to add the MembershipReboot package, subclassing the MembershipReboot factory classes, and then loading your new factories during the idsrv3 startup. You will also need the IdentityServer3.MembershipReboot project, which acts as a go-between between IdentityServer3 and MembershipReboot.
In the visual studio package manager console you add the projects like so:
Install-Package BrockAllen.MembershipReboot
Install-Package IdentityServer3.MembershipReboot
You can use the idsrv3 samples as an example of how to set up your classes. https://github.com/IdentityServer/IdentityServer3.Samples
That will give you the data entities you need. Then to write your entities to a database, add the MembershipReboot.EF project and set up a database connection string that gets passed to your override of the MembershipRebootDbContext() class.
Install-Package BrockAllen.MembershipReboot.Ef
The first time you start your identity server, MembershipReboot.EF will use Entity Framework to automatically create your database schema and start writing your entities there.
Hope that gets you started, sorry if it's not what you're asking!

SQL Local DB Connection on Microsoft SilverLight

Using VB.NET, I can connect to a local SQL database easily, but, trying to get more advanced graphical features, I started using SilverLight for VB.NET, and so i got this problem.
Is it possible to connect a local database, and it must be SQL, with SilverLight for VB.NET?
It must be Out Of Browser too, i'm doing a Desktop App.
If there's no really way to do it, how can i make a more beatiful system, as I don't want to make the same old windows style.
Yes, you can access a db with Silverlight. As to how, the same ways you'd do it without SL really. Personally I use EntityFramework, and create a model from my database. Then I operate on that model using the context generated.
It doesn't matter if the db is local or remote, the methods are the same - they are just connection strings after all.

Creating MVC 4 data access layer using SQL Server 2000

I am a novice ASP.Net developer/student/intern. Currently, I am in the process of creating my first MVC web app with:
VS 2012 Ultimate C#
SQL Server 2000
My database is on a SQL Server 2000 with no chance of upgrading to >= 2005 in the foreseeable future. Thus preventing me from using EF, which all the books and examples I’ve read so far have used.
I have created a C# SQL helper class for connections, commands etc. that I have used previously as a data access layer for other basic web form applications.
What's the best way to incorporate it into my current MVC DAL?
Also,are there any examples or documentation outlining the basic steps in creating a sound MVC DAL that adheres to MVC best practices while using server 2000 and without EF.?
Any suggestions/guidance would be greatly appreciated.
Thank you,
Todd
Technically speaking, MVC has nothing to do with your DAL. As far as your MVC app is concerned, it shops at the repository layer and does not care what happens in the warehouse (data layer).
Your DAL can be built using ADO.NET. Although this technology is old, its perfectly usable. If you check out http://www.dofactory.com/Default.aspx, you will see an app that is build using multiple DAL technologies. One is for Entity Framework and another is for ADO.Net ... and the cool part is they are BOTH hitting the same database.

Can I use pure SQL in ASP.net MVC?

hello guys i need to use pure SQL queries for my database project
at the same time i wanna use ASP.net MVC so i can learn a new technology !!!
can i use SQL with ASP.net MVC WITHOUT using " LINQ to SQL " ?!
i'm still learning so i'm sorry if it so stupid using things the OLD SCHOOL
but my instructor insists that he want to see a PURE SQL Queries
Yes. You can use whatever you want for your model layer. Note, you can use raw SQL queries with LINQ To SQL as well.
Northwnd db = new Northwnd(#"c:\northwnd.mdf");
IEnumerable<Customer> results = db.ExecuteQuery<Customer>
(#"SELECT c1.custid as CustomerID, c2.custName as ContactName
FROM customer1 as c1, customer2 as c2
WHERE c1.custid = c2.custid"
);
Or perhaps your instructor wants straight up ADO.NET, which works fine too.
There is an example here.
You can use raw SQL queries in ASP.Net MVC the same way you use them anywhere else.
It can be helpful to use idioms like
using (var reader = command.ExecuteReader())
return View(reader.Select(dr => new { Name = dr["Name"], ... }));
EDIT: It appears that you're asking how to use ADO.Net in general.
Try this tutorial.
Of course you can use SQL in ASP.NET MVC. I'd consider keeping the SQL code on stored procedures rather than hard-coding the SQL directly in the application code. It's better to maintain and it's a good practice that will probably be looked upon by your instructor.
SQL has nothing to do with ASP.NET MVC. ASP.NET MVC is a web framework for building web sites and applications. My suggestion is that you try to abstract as much as possible, the MVC application shouldn't know if you're using SQL (stored procedures), Entity Framework, Linq-to-sql or if you get your data from a web-service.

Does nHibernate allow drag and drop automatic class creation like linq to sql?

Just a basic question. Learning Linq to SQL and some nHibernate. I am using the mvc tutorial and they drag and drop tables onto the visual studio designer to create the classes and wire up everything.
When I experimented with nHibernate I had to do lots with xml files. Does nHibernate have anything that is "easy" like Linq to SQL or is this drag and drop for Linq to SQL so basic that when I want to do something "real" it won't matter that Visual Studio does this for me (at this basic level)? In other words, the further I go with Linq to SQL, I'll eventually have to handle config files like I do with nHibernate.
Look at Castle's ActiveRecord framework. It replaces the use of XML config files with the use of Attributes directly on the class/property declaration. Also, a tool called ActiveWriter integrates with Visual Studio and allows connecting to a data source and generating the object model!
There is no "native" support like you see with LINQ to SQL. However, there are third party add-ins that will allow you to do something similar with nHibernate. My favorite is this one:
http://sourceforge.net/projects/nhibernateaddin
To use it:
Create a data connection to a
database that contains the structure
you are going to code against (your
development database).
Add a new NHibernate plug-in item
(via add new item) to your project
that will contain you domain objects.
In the property window add the data
connection string from the data
connection you just created (this
isn't automated yet).
Finally, you drag and drop your tables
from your data connection to the
NHibernate plug-in object and when
saved your mapping files and you
domain objects are generated. To use
it you create a data connection to a
database that contains the structure
you are going to code against (your
development database).