ASP.NET MVC 2 - user defined database connection - asp.net

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.

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?

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

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));
}
}
}

Can you insert a new database connection in an existing database connection in OLEDB?

I am creating a program in which I am trying to have a new database connection in an existing database connection in OLEDB using ASP.net. Is that even possible?
"Connection within connection" is likely not what you have in mind. But there is no issue with having both OleDb and SQLServer connections operating together in one ASP.NET application, you can add these 2 or many more connections to one app.
Answer is yes. Please make sure you close the connections after using by using statement or close() explicitly. More here.
using (OleDbConnection connection1 = new OleDbConnection(connectionString1))
using (OleDbConnection connection2 = new OleDbConnection(connectionString2))
{
connection1.Open();
connection2.Open();
// Do something
}

Connecting MS SQL DataBase To GODADDY host

I am new to uploading websites online, I have uploaded my website to Godaddy host and run perfectly but i don't know how to connect my MS Sql Data base and how to configure my connection string ! can any one help me or give me a tutorial of how doing this starting from a normal MS sql Database with normal connection string(Local DB and Local connection string) until uploading it online ?
Connecting to a sql server database on go daddy works the same way as connecting to any other Sql Server db from a .Net perspective.
Log in to your go daddy account to get the connection string info. There's even code samples in there for how to configure your connection string.
Alos, here's a link about .net connection strings in general
http://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx
One of the advantages of using MS SQL is that you can use the SqlClient native libraries, which are faster than ODBC, etc.
using System.Data;
using System.Data.SqlClient;
public class Demo {
public DataTable ConnectAndQuery() {
SqlConnection cn = new SqlConnection("Data Source=IP-GOES-HERE;Initial Catalog=YOUR-DATABASE-NAME-HERE;user id=DB-USER-HERE;password=DB-PASS-HERE;Connection Timeout=300");
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM TableName", cn);
DataSet results = new DataSet();
da.Fill(results);
return results;
}
}
I usually store my connection strings in the web.config. There's actually a specific section you can store them in, if you'd like. Here's an example of that:
http://msdn.microsoft.com/en-us/library/dx0f3cf2(v=vs.85).aspx

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

Resources