View Full Version : Database Programming from ASP Classic to ASP.NET

01-17-2005, 11:36 PM

I've migrated from ASP Classic to ASP.NET but I'm having real trouble learning the new ADO.NET way of database programming. I used to use the following two functions for creating ASP Classic database connections:

Function CreateConn()

dim Connect
dim cnDB
Connect = "Provider=SQLOLEDB.1;Data Source=DBASESVR;Initial Catalog=Contractors;uid=user;pwd=pass"
Set cnDB = Server.CreateObject("ADODB.Connection")
Set CreateConn = cnDB

End Function

Function SQLQuery(cnDB, queryString)

dim rsADO
Set rsADO = Server.CreateObject("ADODB.Recordset")
rsADO.ActiveConnection = cnDB
Set SQLQuery = rsADO

End Function

And then display data to my ASP Classic pages with code like this:

dim cnDB
Set cnDB = CreateConnection

SqlCommandView = "SELECT ID, Name FROM Contacts WHERE ID=1"
Set RsView = SQLQuery(cnDB, SqlCommandView)

Dim DataVariable
DataVariable = RsView("Name")

How do I convert this process to ADO.NET? Help would be muchly appreicated, thanks!


01-18-2005, 10:34 PM
I ended up finding the following code that is working well:

Dim MyConnection As System.Data.SqlClient.SqlConnection
Dim MyCommand As System.Data.SqlClient.SqlCommand
Dim MyReader As System.Data.SqlClient.SqlDataReader

nameSQL = "SELECT Name FROM [BMIS Live Database].dbo.[BMIS Live Database$Vendor] WHERE No_ = '" & Dt(1) & "'"
MyConnection = New System.Data.SqlClient.SqlConnection("server=DBASESVR;uid=CSC;pwd=CSC!;database=BMIS Live Database")
MyCommand = New System.Data.SqlClient.SqlCommand(nameSQL, MyConnection)
MyReader = MyCommand.ExecuteReader


Does anyone know if there is a way to reuse this connection code into a function so I can call it like I did in ASP with the CreateConnection and SQLQuery functions? Thanks.