how do I early bind

M

Martin Eyles

I have just turned on option strict in ASP/VB.net. I started by happily
going through and setting up all of the Dim statements with As clauses, and
making my cast explicit. However, I have now got stuck on "late binding".
After reading that early binding is faster, I think that it would be best to
make everything early binding, but I can't work out how. have but a sample
of my database code below. Can anyone tell me how to fix it?

Thanks,
Martin

Public Sub aSub()
'Allocate Variables
Dim adCmdText, aConnectionString, SQL As String
Dim R, F, RecsAffected As Object
Dim conn As Object

'set connection string for login to database
aConnectionString = "Provider=SQLOLEDB;Data Source=server;" _
& "Database=aDataBase;UID=aUserID;PWD=aPassword"
conn = Server.CreateObject("ADODB.Connection") 'make connection
object
'send connection string to database, via oject
conn.ConnectionString = aConnectionString
'open the connection
conn.Open()

SQL = ("SELECT aValue FROM aTable WHERE theCondition='" + aCondition
+ "'")
R = conn.execute(SQL, RecsAffected, adCmdText)
For Each F In R.Fields
Response.Write("") 'do nothing
Next
While Not R.EOF
For Each F In R.Fields
aValue = F.Value
Next
R.MoveNext()
End While

conn.Close()
End Sub
 
K

Karl Seguin

Marin,
Glad to see you are turning on Option Explicit and Option Strict.

You can early bind like this:

dim connection as System.Data.SqlClient.SqlConnection

connection = new System.Data.SqlClient.SqlConnection

of course, if you Import System.Data.SqlClient at the top of your page, this
looks a lot nicer:

dim connection as SqlConnection
connection = new SqlConnection()

or

dim connection as SqlConnection = new SqlConnection()


also look at exception handling (try/catch) and if you are serious about
properly learning VB.Net, check out
http://www.amazon.com/exec/obidos/ASIN/0321169514/panopticoncen-20/002-7185744-5788008


Karl
 
M

Martin Eyles

I have done that now, and the connection object complained that it didn't
have the execute method. I found there is a SqlCommand class too from an
example in the help, and so have added the following code:-

anSQLCommand = New System.Data.SqlClient.SqlCommand
anSQLCommand.Connection() = conn
anSQLCommand.CommandText = SQL

My problem is that there are four different execute... methods in the
SqlCommand object:- NonQuery, Reader, Scalar and XmlReader. Could you
explain which ones I need, and how to use them? Also, are these ADODB like
my old code, or are they the new ADO.net classes? Does this make any
difference?

Thanks again,
Martin
 
K

Karl Seguin

Sorry, you are right, I changed you over from ADODB to ADO.Net...it does
make a difference. I obviously recommend that you move to ADO.Net, but now
might not be the correct time for you. If you are just doing this for
learning, strongly consider going with ADO.Net, if you have a deadline for a
project and ADODB is working, why not wait 'til downtime before rocking the
boat too much.

To use ADODB in a strongly-typed manner, you'll need to add a reference to
Microsoft ActiveX Data Object Library 2.7. You do this in VS.Net by right
clicking on "referneces", selecting "Add Reference", selecting the "COM" tab
and then picking that class.

Once added, you can do thigs like:
Dim connection As ADODB.Connection = New ADODB.Connection
connection.ConnectionString = ""

or

Dim rs as ADODB.Recordset = .....

and pretty much use it all like you are used to...

Karl
 
M

Martin Eyles

I can afford a bit of time to get it going in ADO.net. Have figured out how
to get one value with scalar now, I think. The only thing now is working
with more data.

I am used to:-

I think the first line should become R=command.executeReader, but what class
of object is R, and what changes do I have to make to the rest?

Thanks,
ME
 
M

Martin Eyles

Thankyou,
I think there is plenty of information to read on this. I had
googled a bit, but it helps to know what you are googling for, so thanks for
the link too.

ME

--
Martin Eyles
(e-mail address removed)

Karl Seguin said:
SqlDataReader dr = command.executeReader()
while dr.Read()
dim someField as string = cstr(dr("someColumn"))
end while

you might wanna check out an introduction on this sorta stuff such as
(http://www.sitepoint.com/article/introduction-ado-net/2) which I just got
from a google search...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Martin Eyles said:
I can afford a bit of time to get it going in ADO.net. Have figured out how
to get one value with scalar now, I think. The only thing now is working
with more data.

I am used to:-


I think the first line should become R=command.executeReader, but what class
of object is R, and what changes do I have to make to the rest?

Thanks,
ME

--
Martin Eyles
(e-mail address removed)

for rocking
the
reference
to "COM"
tab
from
an ADODB
like
http://www.amazon.com/exec/obidos/ASIN/0321169514/panopticoncen-20/002-7185744-5788008
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top