Forum Moderators: open

Message Too Old, No Replies

Connection strings

         

stevelibby

4:20 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



I am a complete newbie to asp code and connections. I have been using frontpage till now. I wish to advance my knowledge a little further. I have spent most of today trying to understand the coding, i kinda get it but i dont. I am sure that once i see the script in a correct order then i am sure i`ll start to understand it better.
So......
I have gathered that this is my connection string and i have tested it and it connects...what next.?
I have database called Keywords and i want extract the simpliest of data...what do i do now? Most stuff that i have read is misleading, so can someone give me some simple coding and just explain a little about it.

<%

Dim conn

Set conn = Server.CreateObject("ADODB.Connection")

conn.open "Provider=sqloledb;Data Source=ip address;Initial Catalog=dbname;User Id=#*$!;Password=#*$!x;"
%>

mattglet

6:15 pm on Feb 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Reading through the tutorials at [w3schools.com...] is really a great place to start. Plenty of very easy to follow and understand examples.

stevelibby

8:08 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



i have tried to read all that but i cant seem to get the hang of it, if i were to see a simple connection, i think it might start to come together. Can u help?

weppos

8:42 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



In order to get data from a db you need to follow these steps:

1. define a connection string depending on the driver and the database type.
You may find some strings here: [connectionstrings.com...]

2. create a connection object, for example

Dim objConn
Set objConn = Server.CreateObject("ADODB.Connection")

3. open the connection

objConn.Open strSQL

where strSQL is the connection string you defined before.

4. query the database

5. close and reset connection object.

Set objConn = Nothing

About the point number 4 it's to long to explain here in some lines.

You need to know something about SQL language to work with the database.
The link mattglet gave to you contains lots of interesting articles and information.

stevelibby

8:55 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



Thank you for that, i have visited this site you have said and aslo phned my host to which they recomended:
"Provider=sqloledb;Data Source=Aron1;Initial Catalog=pubs;User Id=sa;Password=asdasd;"
as my string.

therefore is...

<%

Dim conn

Set conn = Server.CreateObject("ADODB.Connection")

conn.open "Provider=sqloledb;Data Source=ip address;Initial Catalog=dbname;User Id=#*$!;Password=#*$!x;"

Set conn=nothing
%>

so far correct? or should conn be replaced with objconn?

weppos

9:04 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



Yes, it's good.
Conn (or objConn) is just a variable name so can be called also var , convar or something else.

I usually use a prefix before each variable so obj corresponds to object.

stevelibby

9:09 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



Great stuff, i am getting there. now, if i want to query something real simple from the db how, what, when, lets say the table name is keywords and i want to show all info...what do i do now?

Do i keep the above closed and then start a new
<%
%>
But call conn?

This is where i am stuck. Can you giva example?

weppos

9:20 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



<%

...

' Create a recordset object
Set objRs = Server.CreateObject("ADODB.Recordset")

strSQL = "SELECT fieldname FROM tablename"
objRs.Open strSQL, objConn

' do something

objRs.Close

Set objRs = Nothing

...

%>

This is SQL.
[w3schools.com...]

stevelibby

9:30 pm on Feb 23, 2005 (gmt 0)

10+ Year Member



ok do something?, what can i put? if i created a query is access would that be enough to go here?

So far we are at:

<%

Dim conn

Set conn = Server.CreateObject("ADODB.Connection")

conn.open "Provider=sqloledb;Data Source=ip address;Initial Catalog=dbname;User Id=#*$!;Password=#*$!x;"
set objRs = Server.CreateObject("ADODB.Recordset")

strSQL = "SELECT fieldname FROM tablename"
objRs.Open strSQL, objConn

' do something

objRs.Close
Set objRs = Nothing

Set conn=nothing
%>

am i still correct? It the do something bit that it appears im stuck with.

Staffa

12:33 am on Feb 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



' do something

replace with :

response.write (strSQL)

weppos

7:35 pm on Feb 24, 2005 (gmt 0)

10+ Year Member



Do something means that in that place you need to put the code to work with the database.

I can't tell you what you need to put there because it depends what you want to do.

You may print all records, read some data, create an other query...

Before starting create something with ASP you need to know what you want to do.