SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old July 23rd, 2004, 11:20 PM
tr333 tr333 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Location: Sydney, Australia
Posts: 44 tr333 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 58 sec
Reputation Power: 5
sql in asp pages

hi,

could someone please tell me how to execute an sql statement (eg. UPDATE) in an asp page.

do i need to execute the code when opening a recordset, or do i just type the code into the page??


asp newbie..

Reply With Quote
  #2  
Old July 26th, 2004, 06:15 AM
selwonk's Avatar
selwonk selwonk is offline
Contributing User
ASP Free Loyal (3000 - 3499 posts)
 
Join Date: Jun 2004
Posts: 3,048 selwonk User rank is First Lieutenant (10000 - 20000 Reputation Level)selwonk User rank is First Lieutenant (10000 - 20000 Reputation Level)selwonk User rank is First Lieutenant (10000 - 20000 Reputation Level)selwonk User rank is First Lieutenant (10000 - 20000 Reputation Level)selwonk User rank is First Lieutenant (10000 - 20000 Reputation Level)selwonk User rank is First Lieutenant (10000 - 20000 Reputation Level)selwonk User rank is First Lieutenant (10000 - 20000 Reputation Level)selwonk User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 2 h 58 m 53 sec
Reputation Power: 135
Hi

This is a simple example which demonstrates connecting to a remote SQL server:

Code:
<%
 str_ConnectionString = "Driver={SQL Server};" & _
	   "Server=_ipaddress_;" & _
	   "Address=_ipaddress_;" & _
	   "Network=DBMSSOCN;" & _
	   "Database=_database_;" & _
	   "Uid=_username_;" & _
	   "Pwd=_password_;" & _
	   "Network=DBMSSOCN;"
%>
<html>
<head>
<title>Example ASP database update<title>
</head>
<body>
<h3>Updating database</h3>
<%
 ' Objects used for data connection
 Set obj_Connection = Server.CreateObject("ADODB.Connection")
 Set obj_RecordSet  = Server.CreateObject("ADODB.Recordset")
 ' Create database connection
 obj_Connection.Open str_ConnectionString
 ' Create record set
 str_SQL = "INSERT INTO TableName (Field1, Field2) VALUES ('Value1', 'Value2');"
 Set obj_RecordSet = obj_Connection.Execute(str_SQL, adBoolean)
 ' Close the connection
 obj_Connection.Close
  
 ' Destroy the objects used
 Set obj_Connection = Nothing
 Set obj_RecordSet  = Nothing
%>
<h4>Finished</h4>
</body>
</html>


MK

Reply With Quote
  #3  
Old July 26th, 2004, 09:29 AM
Memnoch's Avatar
Memnoch Memnoch is online now
Unholy Moderator
Click here for more information.
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,879 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 18 h 10 m 33 sec
Reputation Power: 500
1) You don't need to use a recordset object unless you are wanting to do things on the client, such as use rs.recordcount.

2) You don't need a recordset object for doing INSERT, UPDATE or DELETE statements. Recordset objects are used to hold records returned from a SELECT statement used to query a database for records.

So although selwonks code would work, it isn't necessary. The same thing can be done with much less code.
Code:
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open "Your ConnectionString"

To Insert Records
Conn.Execute("INSERT INTO TableName VALUES(value1, value2)")

To Return Records
Set rs = Conn.Execute("SELECT * FROM TableName")

if rs.eof then
   Response.write("No records returned")
end if

do until rs.eof
   Response.write(rs("Fieldname"))
   rs.movenext
loop

Conn.Close
Set Conn = Nothing


I never use a recordset object, simply because it adds additional overhead to the server, because it requires the server to create another object in memory.

BTW, if you are connecting to Sql Server, don't use the ODBC driver
Code:
str_ConnectionString = "Driver={SQL Server};" 

use oledb
Code:
str_ConnectionString = "Driver=sqloledb;" 

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > sql in asp pages


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT