With ADO 2.x we instantiate a connection object from which we then call its open method, passing in a connection string. In ADO.NET the same principles apply, instead we instantiate an SqlConnection object, which exists under the System.Data.SqlClient namespace.
Here's how we connect to an SQL Server database on the local machine using classic ASP and ADO 2.6:
<% dim objConn set objConn = Server.CreateObject("ADODB.Connection") objConn.Open "Provider=SQLOLEDB; Data Source=(local); Initial Catalog=Pubs; UId=sa; Pwd=" %> |
Here's an example that does the same thing, only this time we're using ASP.NET and ADO.NET:
<%@ import namespace="System.Data" %> <%@ import namespace="System.Data.SqlClient" %>
<script language="c#" runat="server"> public void Page_Load(object sender, EventArgs e) { SqlConnection objConn = new SqlConnection("Server=(local); Database=Pubs; UId=sa; Pwd=");
objConn.Open(); } </script |
In our ADO.NET example above, we've used the SqlClient namespace. We could just have easily used the OleDb namespace and OleDb connection class to connect to our SQL Server database, because the SQLOLEDB provide is OleDb compatible.
The great thing about ADO.NET is that in 99% of the cases, you can simply copy and paste your old ADO connection strings into your .NET applications and they should work fine.