|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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.. |
|
#2
|
||||
|
||||
|
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 |
|
#3
|
||||
|
||||
|
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;" |
![]() |
| Viewing: ASP Free Forums > Database > SQL Development > sql in asp pages |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|