What is the difference between .net data providers and odbc drivers? - asp.net

Can you please help me understand the difference between these two technologies of connecting to databases?
I am new to the field of database connectivity, but may you can explain on specific examples which technology is used in each case? What for example should I use if I am developing the application ob Visual Studio and going to connect in with MySQL and why?
May be the question will seem stupid for the professionals, but I would be very thankful if you will clarify it for me.

A .NET data provider is a software component that provides access to a data source. They specifically designed to work in .NET applications (C#, VB.NET, etc.) and will have at least some standard classes/functions: Connection, Command, DataReader, DataAdapter (similar to the native System.Data.SQLClient classes).
An ODBC driver is very similar, but is written using the ODBC standard, and while it can be used programmatically in applications, it is also capable of being used as a data connector in any number of popular BI/reporting/ETL tools (like Tableau, QlikView, PowerBI, etc.). To use an ODBC driver in a .NET application, you'll need to use the Microsoft.Data.ODBC library.
For your use case, you'll want to use an ADO.NET data provider so that you can be sure that you're using a product that was specifically designed to be used in .NET applications. Some basic code would look similar to the following:
string connString = "server=XXX.XXX.XXX.XXX;database=myDatabase;user=myUser;password=myPassword;...";
using (MySQLConnection conn = new MySQLConnection(connString){
MySQLCommand cmd = new MySQLCommand("SELECT * FROM some_table", conn);
MySQLDataReader rdr = cmd.ExecuteReader();
while(rdr.Read()) {
Console.WriteLine("=================");
for (int i=0; i<rdr.FieldCount; i++) {
Console.WriteLine(rdr.GetName(i) + ":\t\t" + rdr.GetValue(i));
}
}
}

Related

Avoiding distributed transtactions on connection to multiple DBs on the same SQL server, .net Core

We are migrating a project from .NET Framework to .NET Core, the project is working with multiple data bases that are on the same SQL server. In the past we used a transaction scope for any transaction that we wanted to roll back in case of an error.
When the transaction is involving multiple DBs the transaction is being promoted to a distributed transactions which is not supported in .NET Core.
Question is, if all DBs are actually on the same server, if I will use a 'cross-database queries' like is suggested at the very last part of this Answer will I be insured against such a scenario?
Does 'cross-database queries' simply means running raw-SQL commands like:
using(TransactionScope scope = new TransactionScope())
{
var connection = new SqlConnection(connectionString);
connection.Open();
var SqlComm1 = new SqlCommand("Insert into TableA...", connection);
SqlComm1 .ExecuteNonQuery();
var SqlComm2 = new SqlCommand("Insert into [DB2].[dbo].[TableB]...";
SqlComm2 .ExecuteNonQuery();
.
.
}
if not, can I get a code example of what it actually is?
lastly, while using 'cross-database queries' can I take advantage of anything from my actual DBContexts? like connections, dbSets or anything and if so, how?

SignalR + SQLNotifications in .NetCore

Hi I have a webApi and I want to notify when a record changes in the DB. I'm trying to use SQLNotification but this code generates error
sqlcommand does not contain definition for Notification
using (SqlCommand command = new SqlCommand(query, connection))
{
command.Notification = null;
DataTable dt = new DataTable();
SqlDependency dependency = new SqlDependency(command);
}
Is posible to use SQLNotifications in NetCore 2.0?
I may be mistaken but I don't believe it is available currently based on:
Microsoft's announcement stating:
Simplified Scale-Out Model
Unfortunately, when it comes to scaling out applications there is no “one size fits all” model – each application is different and has different requirements that need to be considered when scaling out the application. We have worked to improve, and simplify, the scale-out model and are providing a Redis based scale-out component in this Alpha. Support for other providers is being evaluated for the final release, for example service bus.
As well as this open issue and specifically David Fowler's comment:
It's just a very very bad way to do real time notifications as that's not what sql was meant for.

Connect to SQL server using Linq, asp.net

I`m creating a website using asp.net, and I need to use a local SQL server (using Microsoft SQL server). And I have created database and tables in it using the MS SQL Server Management Studio.
Now I successfully connect to the database and do some simple add/query using the following commands:
string connectionString = "data source=ABCD\\SQLEXPRESS;initial catalog=PMD;Trusted_Connection=yes;";
string sqlQuery = "INSERT INTO PMD (username, userID, userAddress)";
sqlQuery += " VALUES (#user, id, add)";
SqlConnection dataConnection = new SqlConnection(connectionString);
SqlCommand dataCommand = new SqlCommand(sqlQuery, dataConnection);
dataCommand.Parameters.AddWithValue("user", USER.Value);
dataCommand.Parameters.AddWithValue("id", ID.Value);
dataCommand.Parameters.AddWithValue("add", ADDRESS.Text);
dataConnection.Open();
dataCommand.ExecuteNonQuery();
dataConnection.Close();
The command above can add one column to the table, with values stated.
The query is done in a similar way. Compared with Linq, this is not very concise.
So I was wondering how can I change the code so I can use Linq.
The biggest question for me now is how to connect to the base. I already know all the syntax of Linq.
eg: var query=from c in db.username where c.Contain(“Micheal”) select c (or maybe db.PMD.username)
How can I get the db to link with ABCD/SQLEXPRESS, table PMD?
First you need an Object/Relational Mapper (O/RM). You can't just put LINQ on top of your old ADO.NET code.
Microsoft provides two: Linq2SQL and Entity Framework.
Linq2SQL has been discontinued. If I had to choose between the two, I'd go with Entity Framework.
Here you can find an introduction: http://www.asp.net/entity-framework
For example, install Entity Framework, then connect to sql server with entity framework

asp.net: Is sqlConnection ADO?

Is sqlConnection ADO, if not, what's the name of the layer?
To invoke ADO, (to access non-MS db's), is OleDbConnection the prefered choice?
SqlConnection is part of the ".NET Data Provider for SQL Server", so it is ADO.NET, not to be confused with the old COM-based ADO.
Also, yes I believe OleDBConnection is preferred for Access. I do not believe there is an out-of-the box native data provider for MS Access.
1) The SqlConnection Class is part of the System.Data.SqlClient namespace, and is considered part of ADO.NET.
2) OleDbConnection is for connecting to OLE databases. If you're talking about building a platform-agnostic data access layer, you should use System.Data.Common.DbProviderFactories to create generic DbConnection, DbCommand, etc, objects as in the following code example:
Dim objFactory As DbProviderFactory = DbProviderFactories.GetFactory(ConfigurationManager.AppSettings("DbType"))
Dim objConnection As DbConnection = objFactory.CreateConnection
objConnection.ConnectionString = strConnectionString
In this example, you'd keep the name of the actual provider you're using in your Application Settings.
Note that the generic DB objects only support lowest-common-denominator methods so if you're looking for something platform-specific, you're out of luck. Also of course you will have to specify a valid DBtype to the provider factory, which means you'll have to use either one of the built in providers (ODBC, MS SQL, Oracle, OLEDB) or a third party one. I think you could get away with ODBC if you have a ODBC driver for your platform, but I don't know much about that one.
For information on the different connection objects included in ADO.NET: MSDN-Connecting to Data Sources
Yes it is part of ADO.NET. If you use SqlConnection, actually it is a part of ADO.NET to make a connection between your application and database.

ASP.NET MVC 2 - user defined database connection

I am looking to port my very basic DBMS software from classic ASP to ASP.net - however the user would need to input the connection details in order to connect to their specific DBMS server.
Is this at all possible with ASP.NET MVC (very similar to DSN-less connections in ASP).
Cheers,
Joel
The question should really be "is this possible with .NET", ASP.NET MVC is not a database technology, and DSN-less connections aren't ASP technology either. In .NET, it is the ADO.NET framework that allows you to access database resources, and it can be used from any .NET code, be it desktop, web and mobile too.
There are some specialised libraries for certain platforms, .NET includes native support for Sql Server, you can get the MySql Connector for .NET, etc.
All of these providers are built around the ADO.NET provider model, you can either use them explicitly, or you can use the provider-agnostic method. Here are two examples, the first being Sql Server:
string connectionString = "Server=....";
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand("SELECT [Name] FROM [People]"))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
// Do something here.
}
}
}
In the above example, I'm using the specific Sql Server ADO.NET types to create a connection to a database and execute an arbitrary query against it.
If you are intended to support multiple database platforms, it's probably best to design your code such that it can utilise the ADO.NET Factory classes which are specialised factories geared to the creation of platform specific types. In the example below, I've used the Factory classes to access a MySql Server database:
string connectionString = "Server=....";
DbProviderFactory factory = DbProviderFactories.GetFactory("MySql.Data");
using (DbConnection connection = factory.CreateConnection())
{
connection.ConnectionString = connectionString;
using (DbCommand command = factory.CreateCommand())
{
command.CommandText = "SELECT `Name` FROM Page";
connection.Open();
using (DbDataReader reader = command.ExecuteReader())
{
// Do something here.
}
}
}
Not the perfect example, but enough to get you going, but it's important to remember that DSN-less connections are not tied to ASP or ASP.NET.
Hope that helps.

Resources