|
|
|||||||||
|
|||||||||
|
|||||||||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Looking for a little clarification on ADO...
I was wondering if anyone could clarify a couple things about ADO for me. I've been looking at the tutorials on w3schools and, while they are very thorough, I'm still a little confused.
For example, this snippet from w3schools shows how to connect to an access database: Code:
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
conn.Open "c:/webdata/northwind.mdb"
%>
This code, however, is what I'm currently using. I found it in another tutorial and liked it because of the server.mappath line: Code:
<%
Set Conn = Server.CreateObject("ADODB.Connection")
sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & Server.MapPath("../master.mdb") & ";" & _
"Persist Security Info=True"
Conn.Open(sConnection)
%>
In the w3schools code, they have a conn.provider line and a conn.open line whereas in the code I'm using, the provider and database are in one variable then opened together. Is one more efficient than the other? How would I use the server.mappath command in the w3school example? This next question has more to do with HOW a command functions. The line I use for pulling data from the DB is: Code:
set rs = conn.Execute("SELECT * FROM table ORDER BY " & orderBy)
This lets me pull data from the DB with a simple rs("id") or whatever field I choose. Yet a lot of code samples I see don't use this approach and rather have: Code:
strSQL = "SELECT * FROM table ORDER BY " & orderBy conn.execute strSQL What I'd like to know here is, what exactly does the line I'm using do when I call it? To read rs("id") literally, I'm not seeing where the ("id") part ever comes into play. And how would the second example be better over the one I'm using? I know the code I'm using works great for what I'm doing, but I'd like to know a little more on HOW it works. I also realize these are probably really dense questions and I'm not sure I'll get a reply, but I'd like to thank anyone who takes the time to try explaining it to me. I also have some questions about updating a database, but we'll wait to see how this goes, first. |
|
#2
|
|||
|
|||
|
Looking for a little clarification on ADO...
Bryce,
I'm sure more informed people here will give a more clear & concise answer, but to start the ball rolling here goes: There are two ways to access files on a server, the virtual path and physical path. The virtual path is what you type into a browser and the physical path is: C:\folder\sub-folder\filename.txt So there are times when coding that you want to resolve the virtual path to a physical path and this is where server.mappath comes in to play. Do a search for server.mappath at 4GuysFromRolla for a good article. You then ask about conn.provider: this tells the connection the type of provider/vendor of the database that you're going to talk to, i.e. MS Access, SQL Server, Oracle. You need this provider when communicating with a DB. The difference between w3schools and your code, is that you mention the provider in the connection string. You can use either approach. Finally, you ask the difference between set rs = conn.Execute("SELECT * FROM table ORDER BY " & orderBy) and strSQL = "SELECT * FROM table ORDER BY " & orderBy conn.execute strSQL. As you can probably guess, they're identical. The second approach is better from a code maintainability perspective, because if you have a long SQL string, with many items in the WHERE clause, then you can build the SQL string up on a line by line basis, rather than one long string in the EXECUTE, e.g. strSQL = "SELECT FieldA, FieldB, FieldC, FieldD, FieldE, FieldF, FieldG, FieldH, FieldI, FieldJ, FieldK" strSQL += " WHERE FieldA = FieldB" strSQL += " AND FieldC = FieldD" etc is more readable and maintainable than conn.Execute("SELECT FieldA, FieldB, FieldC, FieldD, FieldE, FieldF, FieldG, FieldH, FieldI, FieldJ, FieldK WHERE FieldA = FieldB AND FieldC = FieldD") Hope that helps. Robert |
![]() |
| Viewing: ASP Free Forums > Other > Programming Help > Looking for a little clarification on ADO... |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|
|