...

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



AlexClifford
01-17-2005, 10:36 PM
Hi,

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")
cnDB.Open(Connect)
Set CreateConn = cnDB

End Function

Function SQLQuery(cnDB, queryString)

dim rsADO
Set rsADO = Server.CreateObject("ADODB.Recordset")
rsADO.ActiveConnection = cnDB
rsADO.Open(queryString)
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!

Alex

AlexClifford
01-18-2005, 09: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")
MyConnection.Open()
MyCommand = New System.Data.SqlClient.SqlCommand(nameSQL, MyConnection)
MyReader = MyCommand.ExecuteReader

Response.Write(MyReader.GetValue(0))

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.

Alex



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum