Most tutorials are Entity Framework with no mention of Ado.Net in .Net Core projects. I have a "legacy" database, so a EF/"Code-First" approach is not an option.
For ADO.NET connections, is the System.Data.SqlClient
available to an ASP.NET Core project?
It is available when I use a .NET Framework project template but is it still available in a .NET Core project?
The ASP.NET CORE framework defines a number of namespaces to interact with a Relational Database System like Microsoft SQL Server, Oracle, MySQL, etc. Collectively, these namespaces are known as ADO.NET.
It isn't obsolete, it is the foundation for working with databases in . NET. It certainly is the archaic way of accessing a database though..
MVC (Model View Controller) is a web application design pattern that is widely used in application development. Here, we are creating an MVC application that connects to the SQL Server with the help of ADO.NET framework. This application contains a Model, a View and a Controller file.
ADO.NET, for WindowsInstall Visual Studio Community, or a similar integrated development environment (IDE) for writing and compiling C# source code. Microsoft now provides Visual Studio Community for free.
The existing SqlConnection
and other related connections still exists within the System.Data.SqlClient
namespace and should work as expected using the full framework or .NET Core.
You'll just need to add the appropriate references and using statements to include it such as through the System.Data.SqlClient
namespace as seen below in your project.json
file :
and then call it via the syntax you are accustomed to :
using(var connection = new SqlConnection("{your-connection-string}")) { // Do work here }
So as long as you have a valid connection string to connect to your existing legacy database, you should be just fine.
Regarding ORM Usage
I also found that some people are using Dapper, a Micro-ORM replacement for Entity Framework, apparenty more flexible. It is there any advantages of using it instead ADO.NET?
These ORMs (object-relational mappers) are handy and often powerful tools that can more easily map your existing database data to specific classes and objects, which can make them easier to use (as opposed to iterating through a data reader, parsing each of your rows and building each object manually).
As far as performance goes, it ultimately depends on what you are going to be doing with your queries. ADO.NET will generally be the fastest as it a bare-bones connection to the database, however in some scenarios Dapper can actually beat it out. Entity Framework, while very useful, generally trails behind in performance, simply because it is such a large ORM.
Again - it ultimately depends on what you are doing, but all are viable options.
.NET Core 2.0 has DataSet, DataTable and SQlDataAdapter. See my answer at https://blogs.msdn.microsoft.com/devfish/2017/05/15/exploring-datatable-and-sqldbadapter-in-asp-net-core-2-0/ .
Code below works fine
public static DataTable ExecuteDataTableSqlDA(SqlConnection conn, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) { System.Data.DataTable dt = new DataTable(); System.Data.SqlClient.SqlDataAdapter da = new SqlDataAdapter(cmdText, conn); da.Fill(dt); return dt; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With